KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > connectionpool > XAPoolDatabaseConnectionFactoryImpl


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: XAPoolDatabaseConnectionFactoryImpl.java,v 1.7 2007/01/08 04:06:25 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.persist.db.connectionpool;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
28 import org.enhydra.jdbc.standard.StandardXADataSource;
29 import org.opensubsystems.core.error.OSSConfigException;
30 import org.opensubsystems.core.error.OSSDatabaseAccessException;
31 import org.opensubsystems.core.error.OSSException;
32 import org.opensubsystems.core.persist.db.DatabaseImpl;
33 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl;
34
35 /**
36  * Implementation of connection pool using Objectweb XAPool package
37  * (http://xapool.experlog.com/).
38  *
39  * @version $Id: XAPoolDatabaseConnectionFactoryImpl.java,v 1.7 2007/01/08 04:06:25 bastafidli Exp $
40  * @author Miro Halas
41  * @code.reviewer Miro Halas
42  * @code.reviewed 1.3 2005/09/24 00:23:57 jlegeny
43  */

44 public class XAPoolDatabaseConnectionFactoryImpl extends PooledDatabaseConnectionFactoryImpl
45 {
46    // Constructors /////////////////////////////////////////////////////////////
47

48    /**
49     * Constructor for new instance using default database properties and default
50     * transaction factory.
51     *
52     * @throws OSSException - an error has occured
53     */

54    public XAPoolDatabaseConnectionFactoryImpl(
55    ) throws OSSException
56    {
57       super();
58       
59       // Do not read the transaction factory here since it may create infinite
60
// loops since some transaction factories such as SimpleLocalTransactionFactoryImpl
61
// asks for default database connection factory
62
// m_transactionFactory = DatabaseTransactionFactoryImpl.getInstance();
63
m_transactionFactory = null;
64
65       // Use the default database properties
66
loadDefaultDatabaseProperties();
67
68       // Do not start it know because when we are creating pool we need to know
69
// what database we are running on and it would create circular dependency
70
// with DatabaseImpl
71
// start();
72
}
73
74    /**
75     * Constructor for new instance using default database properties.
76     *
77     * @param transactionFactory - transaction factory to use for this
78     * connection factory, can be null
79     * @throws OSSConfigException - if there was an error accessing default database
80     * properties
81     * @throws OSSDatabaseAccessException - problem accessing the database
82     */

83    public XAPoolDatabaseConnectionFactoryImpl(
84       DatabaseTransactionFactoryImpl transactionFactory
85    ) throws OSSConfigException,
86             OSSDatabaseAccessException
87    {
88       super(transactionFactory);
89
90       // Use the default database properties
91
loadDefaultDatabaseProperties();
92
93       // Do not start it know because when we are creating pool we need to know
94
// what database we are running on and it would create circular dependency
95
// with DatabaseImpl
96
// start();
97
}
98    
99    /**
100     * Constructor for new instance using explicitely specified properties.
101     *
102     * @param strDriver - JDBC driver to connect to the database
103     * @param strURL - URL of database to connect to
104     * @param strUser - user name to connect to the database
105     * @param strPassword - password to the database
106     * @param transactionFactory - transaction factory to use for this
107     * connection factory, can be null
108     * @throws OSSConfigException - problem accessing or locating the config file.
109     * @throws OSSDatabaseAccessException - problem accessing the database
110     */

111    public XAPoolDatabaseConnectionFactoryImpl(
112       String JavaDoc strDriver,
113       String JavaDoc strURL,
114       String JavaDoc strUser,
115       String JavaDoc strPassword,
116       DatabaseTransactionFactoryImpl transactionFactory
117    ) throws OSSConfigException,
118             OSSDatabaseAccessException
119    {
120       super(strDriver, strURL, strUser, strPassword, transactionFactory);
121
122       // Do not start it know because when we are creating pool we need to know
123
// what database we are running on and it would create circular dependency
124
// with DatabaseImpl
125
// start();
126
}
127
128    // Helper methods ///////////////////////////////////////////////////////////
129

130    /**
131     * {@inheritDoc}
132     */

133    protected Connection JavaDoc getPooledConnection(
134       ConnectionPoolDefinition connectionpool
135    ) throws OSSDatabaseAccessException
136    {
137       Connection JavaDoc conReturn;
138       
139       try
140       {
141          conReturn = (Connection JavaDoc)((StandardXAPoolDataSource)
142                          connectionpool.getConnectionPool()).getConnection();
143       }
144       catch (Exception JavaDoc eExc)
145       {
146          // ObjectPool throws Exception so convert it to something more meaningful here
147
throw new OSSDatabaseAccessException("Cannot get database connection from pool.",
148                                                  eExc);
149       }
150       
151       return conReturn;
152    }
153
154    /**
155     * {@inheritDoc}
156     */

157    protected Connection JavaDoc getPooledConnection(
158       ConnectionPoolDefinition connectionpool,
159       String JavaDoc strUser,
160       String JavaDoc strPassword
161    ) throws OSSDatabaseAccessException
162    {
163       Connection JavaDoc conReturn;
164       
165       try
166       {
167          conReturn = (Connection JavaDoc)((StandardXAPoolDataSource)
168                          connectionpool.getConnectionPool()).getConnection(strUser,
169                                                                            strPassword);
170       }
171       catch (Exception JavaDoc eExc)
172       {
173          // ObjectPool throws Exception so convert it to something more meaningful here
174
throw new OSSDatabaseAccessException("Cannot get database connection from pool.",
175                                                  eExc);
176       }
177       
178       return conReturn;
179    }
180
181    /**
182     * {@inheritDoc}
183     */

184    protected Object JavaDoc createConnectionPool(
185       String JavaDoc strConnectionPoolName,
186       String JavaDoc strDriverName,
187       String JavaDoc strUrl,
188       String JavaDoc strUser,
189       String JavaDoc strPassword
190    ) throws OSSException
191    {
192       PooledDatabaseConnectionFactorySetupReader setupReader
193           = new PooledDatabaseConnectionFactorySetupReader(strConnectionPoolName);
194
195       int iInitialPoolSize = setupReader.getIntegerParameterValue(
196                PooledDatabaseConnectionFactorySetupReader.DBPOOL_INITIAL_SIZE).intValue();
197       int iMinimalPoolSize = setupReader.getIntegerParameterValue(
198                PooledDatabaseConnectionFactorySetupReader.DBPOOL_MIN_SIZE).intValue();
199       int iMaximalPoolSize = setupReader.getIntegerParameterValue(
200                PooledDatabaseConnectionFactorySetupReader.DBPOOL_MAX_SIZE).intValue();
201 // boolean bCanGrow = setupReader.getBooleanParameterValue(
202
// PooledDatabaseConnectionFactorySetupReader.DBPOOL_CAN_GROW).booleanValue();
203
long lMaxWaitTimeForConnection = setupReader.getLongParameterValue(
204                PooledDatabaseConnectionFactorySetupReader.DBPOOL_WAIT_PERIOD).longValue();
205       long lRetryTimeForConnection = setupReader.getLongParameterValue(
206                PooledDatabaseConnectionFactorySetupReader.DBPOOL_RETRY_PERIOD).longValue();
207       int iCheckLevel = setupReader.getIntegerParameterValue(
208                PooledDatabaseConnectionFactorySetupReader.DBPOOL_CHECK_LEVEL).intValue();
209       int iTransactionIsolation
210              = DatabaseImpl.getInstance().getTransactionIsolation(
211                   PooledDatabaseConnectionFactorySetupReader.convertTransactionIsolationToConstant(
212                      setupReader.getStringParameterValue(
213                         PooledDatabaseConnectionFactorySetupReader.DBPOOL_TRANSACTION_ISOLATION
214                            ).toString()));
215       int iPreparedStatementCacheSize = setupReader.getIntegerParameterValue(
216                PooledDatabaseConnectionFactorySetupReader.DBPOOL_PREPSTATEMENT_CACHE_SIZE
217                   ).intValue();
218
219       StandardXADataSource xadsDataSource;
220
221       // Fix for defect# 1630270
222
if (iPreparedStatementCacheSize < 0)
223       {
224          iPreparedStatementCacheSize = 0;
225       }
226       
227       // Create regular XA data source
228
xadsDataSource = new StandardXADataSource();
229       try
230       {
231          xadsDataSource.setDriverName(strDriverName);
232       }
233       catch (SQLException JavaDoc sqleExc)
234       {
235          throw new OSSDatabaseAccessException("Unexpected error when creating" +
236                                                  " connection pool.", sqleExc);
237       }
238       xadsDataSource.setUrl(strUrl);
239       xadsDataSource.setUser(strUser);
240       xadsDataSource.setPassword(strPassword);
241       xadsDataSource.setPreparedStmtCacheSize(iPreparedStatementCacheSize);
242       if (iTransactionIsolation >= 0)
243       {
244          // Set the transaction isolation only if driver supports it
245
xadsDataSource.setTransactionIsolation(iTransactionIsolation);
246       }
247       
248       if (m_transactionFactory != null)
249       {
250          xadsDataSource.setTransactionManager(m_transactionFactory.getTransactionManager());
251       }
252
253       StandardXAPoolDataSource xapdsPoolDataSource;
254       
255       // Create an XA pooled datasource
256
xapdsPoolDataSource = new StandardXAPoolDataSource(iInitialPoolSize);
257       xapdsPoolDataSource.setDataSourceName("XA" + strConnectionPoolName);
258       xapdsPoolDataSource.setDataSource(xadsDataSource);
259       if (m_transactionFactory != null)
260       {
261          xapdsPoolDataSource.setTransactionManager(m_transactionFactory.getTransactionManager());
262       }
263       xapdsPoolDataSource.setUser(strUser);
264       xapdsPoolDataSource.setPassword(strPassword);
265
266       // bCanGrow is not supported, c3p0 pool size cannot grow when all connections
267
// are already requested.
268
try
269       {
270          xapdsPoolDataSource.setMinSize(iMinimalPoolSize);
271          xapdsPoolDataSource.setMaxSize(iMaximalPoolSize);
272       }
273       catch (Exception JavaDoc eExc)
274       {
275          throw new OSSDatabaseAccessException("Unexpected error when creating" +
276                                                  " connection pool.", eExc);
277       }
278       xapdsPoolDataSource.setDeadLockMaxWait(lMaxWaitTimeForConnection);
279       xapdsPoolDataSource.setDeadLockRetryWait(lRetryTimeForConnection);
280       xapdsPoolDataSource.setCheckLevelObject(iCheckLevel);
281       xapdsPoolDataSource.setJdbcTestStmt(
282          DatabaseImpl.getInstance().getConnectionTestStatement());
283
284       return xapdsPoolDataSource;
285    }
286
287    /**
288     * {@inheritDoc}
289     */

290    protected void closeConnectionPool(
291       ConnectionPoolDefinition connectionpool
292    ) throws OSSException
293    {
294       try
295       {
296          ((StandardXAPoolDataSource)connectionpool.getConnectionPool()).shutdown(true);
297       }
298       catch (Exception JavaDoc eExc)
299       {
300          // ObjectPool throws Exception so convert it to something more meaningful here
301
throw new OSSDatabaseAccessException("Cannot close connection pool.", eExc);
302       }
303    }
304 }
305
Popular Tags