KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jodd.db.pool;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5
6 import oracle.jdbc.pool.OracleConnectionCacheImpl;
7
8 /**
9  * Oracle connection pool uses OracleConnectionCacheImpl and adopts it to
10  * ConnectionPool interface.
11  */

12 public class OracleConnectionPool implements ConnectionPool {
13
14     // ---------------------------------------------------------------- properties
15

16     private String url;
17     public void setUrl(String s) {
18         url = s;
19     }
20
21     private String user;
22     public void setUser(String s) {
23         user = s;
24     }
25
26     private String password;
27     public void setPassword(String s) {
28         password = s;
29     }
30
31     private int maxLimit = 10;
32     public void setMaxLimit(int i) {
33         if (i > 0) {
34             maxLimit = i;
35         }
36     }
37
38     private int minLimit = 5;
39     public void setMinLimit(int i) {
40         if (i > maxLimit) {
41             i = maxLimit;
42         }
43         minLimit = i;
44     }
45
46     // ---------------------------------------------------------------- misc
47

48     /**
49      * Return the total no of connections in the Cache.
50      *
51      * @return total no of connections opened.
52      */

53     public int getCacheSize() {
54         return occi.getCacheSize();
55     }
56
57     /**
58      * Returns the total no of connections that are being used.
59      *
60      * @return total no of active connections.
61      */

62     public int getActiveSize() {
63         return occi.getActiveSize();
64     }
65
66     // ---------------------------------------------------------------- init/close
67

68     private OracleConnectionCacheImpl occi = null;
69
70     public OracleConnectionPool() {
71     }
72
73     public void init() throws SQLException {
74         occi = new OracleConnectionCacheImpl();
75         occi.setURL(url);
76         occi.setUser(user);
77         occi.setPassword(password);
78         occi.setMaxLimit(maxLimit);
79         occi.setMinLimit(minLimit);
80     }
81
82     public void close() {
83         try {
84             occi.close();
85         } catch (SQLException sex) {
86         } finally {
87             occi = null;
88         }
89     }
90
91     // ---------------------------------------------------------------- get/free
92

93     /**
94      * Get one free connection from the connection pool.
95      *
96      * @return Connection object representing free database connection
97      * @exception Exception
98      */

99     public Connection getConnection() throws SQLException {
100         return occi.getConnection();
101     }
102
103     /**
104      * Return connection to connection pool
105      *
106      * @param conn database connection
107      */

108     public void freeConnection(Connection conn) {
109         try {
110             if (conn != null) {
111                 conn.close();
112             }
113         } catch (SQLException se) {
114         }
115     }
116
117 }
118
119
Popular Tags