Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc82fed3

Browse files
author
Dave Cramer
committed
Added DataSource code and tests submitted by Aaron Mulder
1 parent6410c22 commitc82fed3

File tree

10 files changed

+1572
-0
lines changed

10 files changed

+1572
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
packageorg.postgresql.jdbc2.optional;
2+
3+
importjavax.naming.*;
4+
importjava.io.PrintWriter;
5+
importjava.sql.*;
6+
7+
/**
8+
* Base class for data sources and related classes.
9+
*
10+
* @author Aaron Mulder (ammulder@chariotsolutions.com)
11+
* @version $Revision: 1.1 $
12+
*/
13+
publicabstractclassBaseDataSourceimplementsReferenceable {
14+
// Load the normal driver, since we'll use it to actually connect to the
15+
// database. That way we don't have to maintain the connecting code in
16+
// multiple places.
17+
static {
18+
try {
19+
Class.forName("org.postgresql.Driver");
20+
}catch (ClassNotFoundExceptione) {
21+
System.err.println("PostgreSQL DataSource unable to load PostgreSQL JDBC Driver");
22+
}
23+
}
24+
25+
// Needed to implement the DataSource/ConnectionPoolDataSource interfaces
26+
privatetransientPrintWriterlogger;
27+
// Don't track loginTimeout, since we'd just ignore it anyway
28+
29+
// Standard properties, defined in the JDBC 2.0 Optional Package spec
30+
privateStringserverName ="localhost";
31+
privateStringdatabaseName;
32+
privateStringuser;
33+
privateStringpassword;
34+
privateintportNumber;
35+
36+
/**
37+
* Gets a connection to the PostgreSQL database. The database is identified by the
38+
* DataSource properties serverName, databaseName, and portNumber. The user to
39+
* connect as is identified by the DataSource properties user and password.
40+
*
41+
* @return A valid database connection.
42+
* @throws SQLException
43+
* Occurs when the database connection cannot be established.
44+
*/
45+
publicConnectiongetConnection()throwsSQLException {
46+
returngetConnection(user,password);
47+
}
48+
49+
/**
50+
* Gets a connection to the PostgreSQL database. The database is identified by the
51+
* DataAource properties serverName, databaseName, and portNumber. The user to
52+
* connect as is identified by the arguments user and password, which override
53+
* the DataSource properties by the same name.
54+
*
55+
* @return A valid database connection.
56+
* @throws SQLException
57+
* Occurs when the database connection cannot be established.
58+
*/
59+
publicConnectiongetConnection(Stringuser,Stringpassword)throwsSQLException {
60+
try {
61+
Connectioncon =DriverManager.getConnection(getUrl(),user,password);
62+
if (logger !=null) {
63+
logger.println("Created a non-pooled connection for " +user +" at " +getUrl());
64+
}
65+
returncon;
66+
}catch (SQLExceptione) {
67+
if (logger !=null) {
68+
logger.println("Failed to create a non-pooled connection for " +user +" at " +getUrl() +": " +e);
69+
}
70+
throwe;
71+
}
72+
}
73+
74+
/**
75+
* This DataSource does not support a configurable login timeout.
76+
* @return 0
77+
*/
78+
publicintgetLoginTimeout()throwsSQLException {
79+
return0;
80+
}
81+
82+
/**
83+
* This DataSource does not support a configurable login timeout. Any value
84+
* provided here will be ignored.
85+
*/
86+
publicvoidsetLoginTimeout(inti)throwsSQLException {
87+
}
88+
89+
/**
90+
* Gets the log writer used to log connections opened.
91+
*/
92+
publicPrintWritergetLogWriter()throwsSQLException {
93+
returnlogger;
94+
}
95+
96+
/**
97+
* The DataSource will note every connection opened to the provided log writer.
98+
*/
99+
publicvoidsetLogWriter(PrintWriterprintWriter)throwsSQLException {
100+
logger =printWriter;
101+
}
102+
103+
/**
104+
* Gets the name of the host the PostgreSQL database is running on.
105+
*/
106+
publicStringgetServerName() {
107+
returnserverName;
108+
}
109+
110+
/**
111+
* Sets the name of the host the PostgreSQL database is running on. If this
112+
* is changed, it will only affect future calls to getConnection. The default
113+
* value is <tt>localhost</tt>.
114+
*/
115+
publicvoidsetServerName(StringserverName) {
116+
if(serverName ==null ||serverName.equals("")) {
117+
this.serverName ="localhost";
118+
}else {
119+
this.serverName =serverName;
120+
}
121+
}
122+
123+
/**
124+
* Gets the name of the PostgreSQL database, running on the server identified
125+
* by the serverName property.
126+
*/
127+
publicStringgetDatabaseName() {
128+
returndatabaseName;
129+
}
130+
131+
/**
132+
* Sets the name of the PostgreSQL database, running on the server identified
133+
* by the serverName property. If this is changed, it will only affect
134+
* future calls to getConnection.
135+
*/
136+
publicvoidsetDatabaseName(StringdatabaseName) {
137+
this.databaseName =databaseName;
138+
}
139+
140+
/**
141+
* Gets a description of this DataSource-ish thing. Must be customized by
142+
* subclasses.
143+
*/
144+
publicabstractStringgetDescription();
145+
146+
/**
147+
* Gets the user to connect as by default. If this is not specified, you must
148+
* use the getConnection method which takes a user and password as parameters.
149+
*/
150+
publicStringgetUser() {
151+
returnuser;
152+
}
153+
154+
/**
155+
* Sets the user to connect as by default. If this is not specified, you must
156+
* use the getConnection method which takes a user and password as parameters.
157+
* If this is changed, it will only affect future calls to getConnection.
158+
*/
159+
publicvoidsetUser(Stringuser) {
160+
this.user =user;
161+
}
162+
163+
/**
164+
* Gets the password to connect with by default. If this is not specified but a
165+
* password is needed to log in, you must use the getConnection method which takes
166+
* a user and password as parameters.
167+
*/
168+
publicStringgetPassword() {
169+
returnpassword;
170+
}
171+
172+
/**
173+
* Sets the password to connect with by default. If this is not specified but a
174+
* password is needed to log in, you must use the getConnection method which takes
175+
* a user and password as parameters. If this is changed, it will only affect
176+
* future calls to getConnection.
177+
*/
178+
publicvoidsetPassword(Stringpassword) {
179+
this.password =password;
180+
}
181+
182+
/**
183+
* Gets the port which the PostgreSQL server is listening on for TCP/IP
184+
* connections.
185+
*
186+
* @return The port, or 0 if the default port will be used.
187+
*/
188+
publicintgetPortNumber() {
189+
returnportNumber;
190+
}
191+
192+
/**
193+
* Gets the port which the PostgreSQL server is listening on for TCP/IP
194+
* connections. Be sure the -i flag is passed to postmaster when PostgreSQL
195+
* is started. If this is not set, or set to 0, the default port will be used.
196+
*/
197+
publicvoidsetPortNumber(intportNumber) {
198+
this.portNumber =portNumber;
199+
}
200+
201+
/**
202+
* Generates a DriverManager URL from the other properties supplied.
203+
*/
204+
privateStringgetUrl() {
205+
return"jdbc:postgresql://"+serverName+(portNumber ==0 ?"" :":"+portNumber)+"/"+databaseName;
206+
}
207+
208+
publicReferencegetReference()throwsNamingException {
209+
Referenceref =newReference(getClass().getName(),PGObjectFactory.class.getName(),null);
210+
ref.add(newStringRefAddr("serverName",serverName));
211+
if (portNumber !=0) {
212+
ref.add(newStringRefAddr("portNumber",Integer.toString(portNumber)));
213+
}
214+
ref.add(newStringRefAddr("databaseName",databaseName));
215+
if (user !=null) {
216+
ref.add(newStringRefAddr("user",user));
217+
}
218+
if (password !=null) {
219+
ref.add(newStringRefAddr("password",password));
220+
}
221+
returnref;
222+
}
223+
224+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
packageorg.postgresql.jdbc2.optional;
2+
3+
importjavax.sql.ConnectionPoolDataSource;
4+
importjavax.sql.PooledConnection;
5+
importjava.sql.SQLException;
6+
importjava.io.Serializable;
7+
8+
/**
9+
* PostgreSQL implementation of ConnectionPoolDataSource. The app server or
10+
* middleware vendor should provide a DataSource implementation that takes advantage
11+
* of this ConnectionPoolDataSource. If not, you can use the PostgreSQL implementation
12+
* known as PoolingDataSource, but that should only be used if your server or middleware
13+
* vendor does not provide their own. Why? The server may want to reuse the same
14+
* Connection across all EJBs requesting a Connection within the same Transaction, or
15+
* provide other similar advanced features.
16+
*
17+
* <p>In any case, in order to use this ConnectionPoolDataSource, you must set the property
18+
* databaseName. The settings for serverName, portNumber, user, and password are
19+
* optional. Note: these properties are declared in the superclass.</p>
20+
*
21+
* <p>This implementation supports JDK 1.3 and higher.</p>
22+
*
23+
* @author Aaron Mulder (ammulder@chariotsolutions.com)
24+
* @version $Revision: 1.1 $
25+
*/
26+
publicclassConnectionPoolextendsBaseDataSourceimplementsSerializable,ConnectionPoolDataSource {
27+
privatebooleandefaultAutoCommit =false;
28+
29+
/**
30+
* Gets a description of this DataSource.
31+
*/
32+
publicStringgetDescription() {
33+
return"ConnectionPoolDataSource from "+org.postgresql.Driver.getVersion();
34+
}
35+
36+
/**
37+
* Gets a connection which may be pooled by the app server or middleware
38+
* implementation of DataSource.
39+
*
40+
* @throws java.sql.SQLException
41+
* Occurs when the physical database connection cannot be established.
42+
*/
43+
publicPooledConnectiongetPooledConnection()throwsSQLException {
44+
returnnewPooledConnectionImpl(getConnection(),defaultAutoCommit);
45+
}
46+
47+
/**
48+
* Gets a connection which may be pooled by the app server or middleware
49+
* implementation of DataSource.
50+
*
51+
* @throws java.sql.SQLException
52+
* Occurs when the physical database connection cannot be established.
53+
*/
54+
publicPooledConnectiongetPooledConnection(Stringuser,Stringpassword)throwsSQLException {
55+
returnnewPooledConnectionImpl(getConnection(user,password),defaultAutoCommit);
56+
}
57+
58+
/**
59+
* Gets whether connections supplied by this pool will have autoCommit
60+
* turned on by default. The default value is <tt>false</tt>, so that
61+
* autoCommit will be turned off by default.
62+
*/
63+
publicbooleanisDefaultAutoCommit() {
64+
returndefaultAutoCommit;
65+
}
66+
67+
/**
68+
* Sets whether connections supplied by this pool will have autoCommit
69+
* turned on by default. The default value is <tt>false</tt>, so that
70+
* autoCommit will be turned off by default.
71+
*/
72+
publicvoidsetDefaultAutoCommit(booleandefaultAutoCommit) {
73+
this.defaultAutoCommit =defaultAutoCommit;
74+
}
75+
76+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
packageorg.postgresql.jdbc2.optional;
2+
3+
importjavax.naming.spi.ObjectFactory;
4+
importjavax.naming.*;
5+
importjava.util.Hashtable;
6+
7+
/**
8+
* Returns a DataSource-ish thing based on a JNDI reference. In the case of a
9+
* SimpleDataSource or ConnectionPool, a new instance is created each time, as
10+
* there is no connection state to maintain. In the case of a PoolingDataSource,
11+
* the same DataSource will be returned for every invocation within the same
12+
* VM/ClassLoader, so that the state of the connections in the pool will be
13+
* consistent.
14+
*
15+
* @author Aaron Mulder (ammulder@chariotsolutions.com)
16+
* @version $Revision: 1.1 $
17+
*/
18+
publicclassPGObjectFactoryimplementsObjectFactory {
19+
/**
20+
* Dereferences a PostgreSQL DataSource. Other types of references are
21+
* ignored.
22+
*/
23+
publicObjectgetObjectInstance(Objectobj,Namename,ContextnameCtx,
24+
Hashtableenvironment)throwsException {
25+
Referenceref = (Reference)obj;
26+
if(ref.getClassName().equals(SimpleDataSource.class.getName())) {
27+
returnloadSimpleDataSource(ref);
28+
}elseif (ref.getClassName().equals(ConnectionPool.class.getName())) {
29+
returnloadConnectionPool(ref);
30+
}elseif (ref.getClassName().equals(PoolingDataSource.class.getName())) {
31+
returnloadPoolingDataSource(ref);
32+
}else {
33+
returnnull;
34+
}
35+
}
36+
37+
privateObjectloadPoolingDataSource(Referenceref) {
38+
// If DataSource exists, return it
39+
Stringname =getProperty(ref,"dataSourceName");
40+
PoolingDataSourcepds =PoolingDataSource.getDataSource(name);
41+
if(pds !=null) {
42+
returnpds;
43+
}
44+
// Otherwise, create a new one
45+
pds =newPoolingDataSource();
46+
pds.setDataSourceName(name);
47+
loadBaseDataSource(pds,ref);
48+
Stringmin =getProperty(ref,"initialConnections");
49+
if (min !=null) {
50+
pds.setInitialConnections(Integer.parseInt(min));
51+
}
52+
Stringmax =getProperty(ref,"maxConnections");
53+
if (max !=null) {
54+
pds.setMaxConnections(Integer.parseInt(max));
55+
}
56+
returnpds;
57+
}
58+
59+
privateObjectloadSimpleDataSource(Referenceref) {
60+
SimpleDataSourceds =newSimpleDataSource();
61+
returnloadBaseDataSource(ds,ref);
62+
}
63+
64+
privateObjectloadConnectionPool(Referenceref) {
65+
ConnectionPoolcp =newConnectionPool();
66+
returnloadBaseDataSource(cp,ref);
67+
}
68+
69+
privateObjectloadBaseDataSource(BaseDataSourceds,Referenceref) {
70+
ds.setDatabaseName(getProperty(ref,"databaseName"));
71+
ds.setPassword(getProperty(ref,"password"));
72+
Stringport =getProperty(ref,"portNumber");
73+
if(port !=null) {
74+
ds.setPortNumber(Integer.parseInt(port));
75+
}
76+
ds.setServerName(getProperty(ref,"serverName"));
77+
ds.setUser(getProperty(ref,"user"));
78+
returnds;
79+
}
80+
81+
privateStringgetProperty(Referenceref,Strings) {
82+
RefAddraddr =ref.get(s);
83+
if(addr ==null) {
84+
returnnull;
85+
}
86+
return (String)addr.getContent();
87+
}
88+
89+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp