Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9142ca2

Browse files
author
Peter Mount
committed
Minor fixes...
1 parent e2e84a1 commit 9142ca2

File tree

3 files changed

+140
-40
lines changed

3 files changed

+140
-40
lines changed

src/interfaces/jdbc/org/postgresql/Driver.java.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public class Driver implements java.sql.Driver
3535
// my early jdbc work did - and that was based on other examples).
3636
// Placing it here, means that the driver is registered once only.
3737
java.sql.DriverManager.registerDriver(new Driver());
38+
39+
// New in 7.1 - register ourselves with the JVM - JDK1.3+ only
40+
@JDK1.3ONLY@org.postgresql.core.ConnectionHook.init();
41+
3842
} catch (SQLException e) {
3943
e.printStackTrace();
4044
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.postgresql.core;
2+
3+
import java.sql.SQLException;
4+
import java.util.ArrayList;
5+
import java.util.Iterator;
6+
import org.postgresql.Connection;
7+
8+
/**
9+
* ConnectionHook keeps track of all open Connections. It's used only in
10+
* Java2 (JDK1.3+) VM's, and it's purpose is to close all connections cleanly
11+
* when the VM terminates.
12+
*
13+
* Important: This only works for JDK1.3 or later as it uses methods new to
14+
* that JDK.
15+
*
16+
* This is a singleton object ;-)
17+
*
18+
* How it works: This is an initiated but un-started Thread. When it's created,
19+
* it registers it'self with the Runtime.addShutdownHook() method.
20+
*
21+
* When a Connection is made, two static methods in org.postgresql.Driver are
22+
* called. For pre JDK1.3 these are noops, but for 1.3+ ANT adds calls to
23+
* methods in this class, which add/remove it from an ArrayList.
24+
*
25+
* Now when the VM terminates it starts this thread, which then Itterates
26+
* through the ArrayList and closes each Connection.
27+
*
28+
* Obviously this doesn't trap things like Runtime.halt() or SIGKILL etc, but
29+
* this captures 99% of all other forms of VM termination.
30+
*
31+
*/
32+
33+
public class ConnectionHook implements Runnable
34+
{
35+
/**
36+
* This ensures that the hook is created and the system is notified of it.
37+
*
38+
* Important: We have to use an instance, as we have to pass a reference to
39+
* the VM.
40+
*/
41+
private static final ConnectionHook SINGLETON = new ConnectionHook();
42+
43+
/**
44+
* The currently open connections
45+
*/
46+
private ArrayList cons = new ArrayList();
47+
48+
/**
49+
* Constructor. This is private because we are a singleton. Here we set
50+
* our selves up, and then register with the VM.
51+
*/
52+
private ConnectionHook() {
53+
super();
54+
Runtime.getRuntime().addShutdownHook(new Thread(this));
55+
}
56+
57+
/**
58+
* Called by Driver, this simply forces us to be created.
59+
*/
60+
public static final void init() {
61+
}
62+
63+
/**
64+
* This is used by org.postgresql.Connection to register itself. Because it's
65+
* called internally, we don't bother with checking to see if it's already
66+
* present (performance boost).
67+
*/
68+
public static final void open(Connection con) {
69+
SINGLETON.cons.add(con);
70+
}
71+
72+
/**
73+
* This is used by org.postgresql.Connection to remove itself.
74+
*/
75+
public static final void close(Connection con) {
76+
SINGLETON.cons.remove(con);
77+
}
78+
79+
/**
80+
* This is called by the VM when it terminates. It itterates through the list
81+
* of connections and implicitly closes them.
82+
*/
83+
public void run() {
84+
Iterator i = cons.iterator();
85+
while(i.hasNext()) {
86+
Connection c = (Connection) i.next();
87+
try {
88+
c.close();
89+
} catch(SQLException e) {
90+
// Ignore as at this point we are dying anyhow ;-)
91+
}
92+
}
93+
}
94+
95+
}

src/interfaces/jdbc/org/postgresql/jdbc2/CallableStatement.java

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
* Parameters are refered to sequentially, by number. The first parameter
2020
* is 1.
2121
*
22-
* {?= call <procedure-name>[<arg1>,<arg2>, ...]}
23-
* {call <procedure-name>[<arg1>,<arg2>, ...]}
22+
* {?= call <procedure-name>[<arg1>,<arg2>, ...]}
23+
* {call <procedure-name>[<arg1>,<arg2>, ...]}
2424
*
2525
*
2626
* <p>IN parameter values are set using the set methods inherited from
@@ -32,8 +32,8 @@
3232
* Multiple ResultSets are handled using operations inherited from
3333
* Statement.
3434
*
35-
* <p>For maximum portability, a call's ResultSets and update counts should
36-
* be processed prior to getting the values of output parameters.
35+
* <p>For maximum portability, a call's ResultSets and update counts should
36+
* be processed prior to getting the values of output parameters.
3737
*
3838
* @see Connection#prepareCall
3939
* @see ResultSet
@@ -48,7 +48,7 @@ public CallableStatement(Connection c,String q) throws SQLException
4848
{
4949
super(c,q);
5050
}
51-
51+
5252
/**
5353
* Before executing a stored procedure call you must explicitly
5454
* call registerOutParameter to register the java.sql.Type of each
@@ -66,7 +66,7 @@ public CallableStatement(Connection c,String q) throws SQLException
6666
*/
6767
public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException {
6868
}
69-
69+
7070
/**
7171
* You must also specify the scale for numeric/decimal types:
7272
*
@@ -84,12 +84,12 @@ public void registerOutParameter(int parameterIndex, int sqlType,
8484
int scale) throws SQLException
8585
{
8686
}
87-
87+
8888
// Old api?
8989
//public boolean isNull(int parameterIndex) throws SQLException {
9090
//return true;
9191
//}
92-
92+
9393
/**
9494
* An OUT parameter may have the value of SQL NULL; wasNull
9595
* reports whether the last value read has this special value.
@@ -103,12 +103,12 @@ public boolean wasNull() throws SQLException {
103103
// check to see if the last access threw an exception
104104
return false; // fake it for now
105105
}
106-
106+
107107
// Old api?
108108
//public String getChar(int parameterIndex) throws SQLException {
109109
//return null;
110110
//}
111-
111+
112112
/**
113113
* Get the value of a CHAR, VARCHAR, or LONGVARCHAR parameter as a
114114
* Java String.
@@ -123,11 +123,11 @@ public String getString(int parameterIndex) throws SQLException {
123123
//public String getVarChar(int parameterIndex) throws SQLException {
124124
// return null;
125125
//}
126-
126+
127127
//public String getLongVarChar(int parameterIndex) throws SQLException {
128128
//return null;
129129
//}
130-
130+
131131
/**
132132
* Get the value of a BIT parameter as a Java boolean.
133133
*
@@ -138,7 +138,7 @@ public String getString(int parameterIndex) throws SQLException {
138138
public boolean getBoolean(int parameterIndex) throws SQLException {
139139
return false;
140140
}
141-
141+
142142
/**
143143
* Get the value of a TINYINT parameter as a Java byte.
144144
*
@@ -149,7 +149,7 @@ public boolean getBoolean(int parameterIndex) throws SQLException {
149149
public byte getByte(int parameterIndex) throws SQLException {
150150
return 0;
151151
}
152-
152+
153153
/**
154154
* Get the value of a SMALLINT parameter as a Java short.
155155
*
@@ -160,7 +160,7 @@ public byte getByte(int parameterIndex) throws SQLException {
160160
public short getShort(int parameterIndex) throws SQLException {
161161
return 0;
162162
}
163-
163+
164164
/**
165165
* Get the value of an INTEGER parameter as a Java int.
166166
*
@@ -171,7 +171,7 @@ public short getShort(int parameterIndex) throws SQLException {
171171
public int getInt(int parameterIndex) throws SQLException {
172172
return 0;
173173
}
174-
174+
175175
/**
176176
* Get the value of a BIGINT parameter as a Java long.
177177
*
@@ -182,7 +182,7 @@ public int getInt(int parameterIndex) throws SQLException {
182182
public long getLong(int parameterIndex) throws SQLException {
183183
return 0;
184184
}
185-
185+
186186
/**
187187
* Get the value of a FLOAT parameter as a Java float.
188188
*
@@ -193,7 +193,7 @@ public long getLong(int parameterIndex) throws SQLException {
193193
public float getFloat(int parameterIndex) throws SQLException {
194194
return (float) 0.0;
195195
}
196-
196+
197197
/**
198198
* Get the value of a DOUBLE parameter as a Java double.
199199
*
@@ -204,7 +204,7 @@ public float getFloat(int parameterIndex) throws SQLException {
204204
public double getDouble(int parameterIndex) throws SQLException {
205205
return 0.0;
206206
}
207-
207+
208208
/**
209209
* Get the value of a NUMERIC parameter as a java.math.BigDecimal
210210
* object.
@@ -214,12 +214,13 @@ public double getDouble(int parameterIndex) throws SQLException {
214214
* desired number of digits to the right of the decimal point
215215
* @return the parameter value; if the value is SQL NULL, the result is null
216216
* @exception SQLException if a database-access error occurs.
217+
* @deprecated in Java2.0
217218
*/
218219
public BigDecimal getBigDecimal(int parameterIndex, int scale)
219220
throws SQLException {
220221
return null;
221222
}
222-
223+
223224
/**
224225
* Get the value of a SQL BINARY or VARBINARY parameter as a Java
225226
* byte[]
@@ -231,12 +232,12 @@ public BigDecimal getBigDecimal(int parameterIndex, int scale)
231232
public byte[] getBytes(int parameterIndex) throws SQLException {
232233
return null;
233234
}
234-
235+
235236
// New API (JPM) (getLongVarBinary)
236237
//public byte[] getBinaryStream(int parameterIndex) throws SQLException {
237238
//return null;
238239
//}
239-
240+
240241
/**
241242
* Get the value of a SQL DATE parameter as a java.sql.Date object
242243
*
@@ -247,7 +248,7 @@ public byte[] getBytes(int parameterIndex) throws SQLException {
247248
public java.sql.Date getDate(int parameterIndex) throws SQLException {
248249
return null;
249250
}
250-
251+
251252
/**
252253
* Get the value of a SQL TIME parameter as a java.sql.Time object.
253254
*
@@ -258,7 +259,7 @@ public java.sql.Date getDate(int parameterIndex) throws SQLException {
258259
public java.sql.Time getTime(int parameterIndex) throws SQLException {
259260
return null;
260261
}
261-
262+
262263
/**
263264
* Get the value of a SQL TIMESTAMP parameter as a java.sql.Timestamp object.
264265
*
@@ -270,16 +271,16 @@ public java.sql.Timestamp getTimestamp(int parameterIndex)
270271
throws SQLException {
271272
return null;
272273
}
273-
274+
274275
//----------------------------------------------------------------------
275276
// Advanced features:
276-
277-
// You can obtain a ParameterMetaData object to get information
277+
278+
// You can obtain a ParameterMetaData object to get information
278279
// about the parameters to this CallableStatement.
279280
//public DatabaseMetaData getMetaData() {
280281
//return null;
281282
//}
282-
283+
283284
// getObject returns a Java object for the parameter.
284285
// See the JDBC spec's "Dynamic Programming" chapter for details.
285286
/**
@@ -304,58 +305,58 @@ public Object getObject(int parameterIndex)
304305
throws SQLException {
305306
return null;
306307
}
307-
308+
308309
// ** JDBC 2 Extensions **
309-
310+
310311
public Array getArray(int i) throws SQLException
311312
{
312313
throw org.postgresql.Driver.notImplemented();
313314
}
314-
315+
315316
public java.math.BigDecimal getBigDecimal(int i) throws SQLException
316317
{
317318
throw org.postgresql.Driver.notImplemented();
318319
}
319-
320+
320321
public Blob getBlob(int i) throws SQLException
321322
{
322323
throw org.postgresql.Driver.notImplemented();
323324
}
324-
325+
325326
public Clob getClob(int i) throws SQLException
326327
{
327328
throw org.postgresql.Driver.notImplemented();
328329
}
329-
330+
330331
public Object getObject(int i,java.util.Map map) throws SQLException
331332
{
332333
throw org.postgresql.Driver.notImplemented();
333334
}
334-
335+
335336
public Ref getRef(int i) throws SQLException
336337
{
337338
throw org.postgresql.Driver.notImplemented();
338339
}
339-
340+
340341
public java.sql.Date getDate(int i,java.util.Calendar cal) throws SQLException
341342
{
342343
throw org.postgresql.Driver.notImplemented();
343344
}
344-
345+
345346
public Time getTime(int i,java.util.Calendar cal) throws SQLException
346347
{
347348
throw org.postgresql.Driver.notImplemented();
348349
}
349-
350+
350351
public Timestamp getTimestamp(int i,java.util.Calendar cal) throws SQLException
351352
{
352353
throw org.postgresql.Driver.notImplemented();
353354
}
354-
355+
355356
public void registerOutParameter(int parameterIndex, int sqlType,String typeName) throws SQLException
356357
{
357358
throw org.postgresql.Driver.notImplemented();
358359
}
359-
360+
360361
}
361362

0 commit comments

Comments
 (0)