KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > connection > ConnectionPoolDataSourceConnectionProvider


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.db.connection;
4
5 import jodd.db.DbSqlException;
6
7 import javax.sql.ConnectionPoolDataSource JavaDoc;
8 import javax.sql.PooledConnection JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.SQLException JavaDoc;
11
12 /**
13  * Connection pool data source {@link jodd.db.connection.ConnectionProvider}
14  */

15 public class ConnectionPoolDataSourceConnectionProvider implements ConnectionProvider {
16     // ---------------------------------------------------------------- properties
17

18     private ConnectionPoolDataSource JavaDoc cpds;
19
20     public ConnectionPoolDataSourceConnectionProvider(ConnectionPoolDataSource JavaDoc cpds) {
21         this.cpds = cpds;
22     }
23
24     // ---------------------------------------------------------------- init/close
25

26     public void init() {
27     }
28
29     public void close() {
30         cpds = null;
31     }
32
33     // ---------------------------------------------------------------- get/free
34

35     public Connection JavaDoc getConnection() {
36         PooledConnection JavaDoc pconn;
37         try {
38             pconn = cpds.getPooledConnection();
39         } catch (SQLException JavaDoc sex) {
40             throw new DbSqlException("Unable to get pooled connection.", sex);
41         }
42         try {
43             return pconn.getConnection();
44         } catch (SQLException JavaDoc sex) {
45             throw new DbSqlException("Unable to get connection from pooled connection.", sex);
46         }
47     }
48
49     public void closeConnection(Connection JavaDoc conn) {
50         try {
51             if (conn != null) {
52                 conn.close();
53             }
54         } catch (SQLException JavaDoc sex) {
55             // ignore
56
}
57     }
58 }
59
Popular Tags