KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > pool > ContextConnectionPool


1 package jodd.db.pool;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5
6 import javax.naming.Context;
7 import javax.naming.InitialContext;
8 import javax.naming.NamingException;
9 import javax.sql.DataSource;
10
11 /**
12  * Tomcat4 alike connection pools that uses Context for initilization.
13  */

14 public class ContextConnectionPool implements ConnectionPool {
15
16     // ---------------------------------------------------------------- pool properties
17

18     private String envContextName;
19     public void setEnvContextName(String s) {
20         envContextName = s;
21     }
22
23     private String dataSourceName;
24     public void setDataSourceName(String s) {
25         dataSourceName = s;
26     }
27
28     // ---------------------------------------------------------------- init/close
29

30     private DataSource ds;
31
32     public void init() throws SQLException {
33         try {
34             Context initContext = new InitialContext();
35             Context envContext = (Context) initContext.lookup(envContextName);
36             ds = (DataSource) envContext.lookup(dataSourceName);
37         } catch (NamingException ne) {
38             throw new SQLException("Context can not be found: " + envContextName + ", " + dataSourceName);
39         }
40     }
41
42     public void close() {
43     }
44
45
46     // ---------------------------------------------------------------- get/free
47

48     public Connection getConnection() throws SQLException {
49         return ds.getConnection();
50     }
51
52     /**
53      * Return connection to connection pool
54      *
55      * @param conn database connection
56      */

57     public void freeConnection(Connection conn) {
58         try {
59             if (conn != null) {
60                 conn.close();
61             }
62         } catch (SQLException e) {
63         }
64     }
65
66
67 }
68
69
Popular Tags