1 package org.opensync.tools;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6 import java.sql.Statement;
7 import java.util.Enumeration;
8 import java.util.Vector;
9
10 public class ConnectionPool {
11 private int checkedOut;
12 private Vector freeConnections = new Vector();
13
14 private String url;
15 private String user;
16 private String password;
17 private int maxConns;
18 private int timeOut;
19 private String logfile;
20 private String conversion_function;
21 private String operator_concat;
22 private boolean autocommit = false;
23 private boolean batchmode = false;
24
25 public ConnectionPool (String url, String user, String password,
26 int maxConns, int initConns, int timeOut, String logFilename,String operator_concat,
27 String conversion_function)throws SQLException{
28 if (Utils.debug) System.out.println("ConnectionPool::ConnectionPool begin");
29 this.conversion_function = conversion_function;
30 this.operator_concat = operator_concat;
31 this.url = url;
32 this.user = user;
33 this.password = password;
34 this.maxConns = maxConns;
35 this.timeOut = timeOut > 0 ? timeOut : 5;
36 this.logfile = logFilename;
37
38 initPool(initConns,logfile);
39 if (Utils.debug) System.out.println("Pool created...");
40 }
41
42 private void initPool(int initConns, String logFile)throws SQLException {
43 for (int i=0;i<initConns;i++) {
44 Connection pc = newConnection();
45 freeConnections.addElement(pc);
46 }
47 }
48
49 public void setAutocommit(boolean autocommit) {
50 this.autocommit = autocommit;
51 }
52
53 public void setBatchmode(boolean batchmode) {
54 this.batchmode = batchmode;
55 }
56
57 public boolean getBatchmode() {
58 return batchmode;
59 }
60
61 public Connection getConnection() throws SQLException {
62 if (Utils.debug) System.out.println("connection request received...");
63 return getConnection(timeOut*1000);
64 }
65
66 private synchronized Connection getConnection(long timeout) throws SQLException {
67 long startTime = System.currentTimeMillis();
68 long remaining = timeout;
69 Connection conn = null;
70
71 while ((conn = getPooledConnection())==null) {
72 try {
73 wait(remaining);
74 }
75 catch (InterruptedException e) {
76 Utils.LogToFile(logfile,"InterruptedException: " + e.getMessage());
77 }
78 remaining = timeout - (System.currentTimeMillis() - startTime);
79 if (remaining <= 0) {
80 throw new SQLException("getConnection() timeout");
81 }
82 }
83
84 if (!isConnectionOK(conn)) {
85 return getConnection(remaining);
86 }
87
88 checkedOut++;
89 conn.setAutoCommit(autocommit);
90
91
92 try {
93 if (!autocommit) conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
94 } catch (SQLException localsqlex) {
95
96 }
97
98
99 return conn;
100 }
101
102 private boolean isConnectionOK(Connection conn) {
103 Statement testStmt = null;
104
105 try {
106 if (!conn.isClosed()) {
107 testStmt = conn.createStatement();
108 testStmt.close();
109 }
110 else return false;
111 }
112 catch (SQLException e) {
113 Utils.SqlLogToFile("SQLException: " + e.getMessage());
114 if (testStmt != null) {
115 try {
116 testStmt.close();
117 }
118 catch (SQLException se) {
119 Utils.SqlLogToFile("SQLException: " + se.getMessage());
120 }
121 }
122 return false;
123 }
124
125 return true;
126 }
127
128 private Connection getPooledConnection() throws SQLException {
129
130
131
132 Connection conn = null;
133
134
135
136
137
138
139
140
141
142
143 if (freeConnections.size() > 0) {
144 conn = (Connection)freeConnections.firstElement();
145 freeConnections.removeElementAt(0);
146 }
147 else if (maxConns == 0 || checkedOut < maxConns){
148
149 System.out.println("Connections used: "+checkedOut+ " free connections: "+ (maxConns > 0 ? maxConns - checkedOut : 0));
150 conn = newConnection();
151 }
152 return conn;
153 }
154
155 private Connection newConnection() throws SQLException {
156
157 if (Utils.debug) System.out.println("ConnectionPool::newConnection call");
158
159 Connection conn = null;
160 if (user == null) conn = DriverManager.getConnection(url);
161 else conn = DriverManager.getConnection(url,user,password);
162 return conn;
163 }
164
165 public synchronized void freeConnection(Connection conn) {
166
167 try {
168 if (!autocommit) conn.commit();
169 } catch (SQLException sqlex) {}
170 if (Utils.debug) System.out.println("ConnectionPool::freeConnection call");
171 freeConnections.addElement(conn);
172 checkedOut--;
173 notifyAll();
174 }
175
176 public synchronized void release()throws SQLException{
177 Enumeration allConnections = freeConnections.elements();
178 while (allConnections.hasMoreElements()) {
179 Connection con = (Connection)allConnections.nextElement();
180
181 if (this.isConnectionOK(con) || !this.autocommit) con.commit();
182 con.close();
183 }
184 freeConnections.removeAllElements();
185 }
186
187 public synchronized void releaseWithException()throws SQLException{
188 Enumeration allConnections = freeConnections.elements();
189 while (allConnections.hasMoreElements()) {
190 Connection con = (Connection)allConnections.nextElement();
191
192 if (this.isConnectionOK(con) || !this.autocommit) con.rollback();
193 con.close();
194 }
195 freeConnections.removeAllElements();
196 }
197
198
199 public String getConversionFunction() {
200 return conversion_function;
201 }
202 public String getOperatorConcat() {
203 return operator_concat;
204 }
205
206 public String getUrl(){
207 return url;
208 }
209 }