ConnectionPerThreadDataSourceFilter.java

biz/hammurapi/sql/ConnectionPerThreadDataSourceFilter.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 082 Parenthesis are redundant. 2 169:46
Java Inspector 089 Undocumented method 2 56:17
Java Inspector 089 Constructor documentation is too short. It is only 1 words. Should be at least 3 words. 2 69:9
Java Inspector 089 Javadoc contains tag for non-existent parameter initConnectionTransaction 2 69:9
Java Inspector 089 Undocumented method 2 73:9
Java Inspector 089 Undocumented method 2 77:9
Java Inspector 089 Undocumented method 2 81:9
Java Inspector 089 Undocumented method 2 85:9
Java Inspector 089 Undocumented method 2 89:9
Java Inspector 089 Undocumented method 2 98:9
Java Inspector 089 Undocumented method 2 120:17
Java Inspector 089 Undocumented method 2 123:17
Java Inspector 089 Undocumented method 2 131:17
Java Inspector 089 Undocumented method 2 134:17
Java Inspector 089 Undocumented method 2 137:17
Java Inspector 089 Undocumented method 2 143:17
Java Inspector 089 Undocumented method 2 147:17
Java Inspector 089 Undocumented method 2 150:17
Java Inspector 089 Undocumented method 2 153:17
Java Inspector 089 Undocumented method 2 156:17
Java Inspector 089 Undocumented method 2 159:17
Java Inspector 089 Undocumented method 2 162:17
Java Inspector 089 Undocumented method 2 165:17
Java Inspector 089 Undocumented method 2 168:17
Java Inspector 089 Undocumented method 2 171:17
Java Inspector 089 Undocumented method 2 174:17
Java Inspector 089 Undocumented method 2 177:17
Java Inspector 089 Undocumented method 2 183:17
Java Inspector 089 Undocumented method 2 187:17
Java Inspector 089 Undocumented method 2 190:17
Java Inspector 089 Undocumented method 2 196:17
Java Inspector 089 Undocumented method 2 202:17
Java Inspector 089 Undocumented method 2 206:17
Java Inspector 089 Undocumented method 2 210:17
Java Inspector 089 Undocumented method 2 214:17
Java Inspector 089 Undocumented method 2 218:17
Java Inspector 089 Undocumented method 2 221:17
Java Inspector 089 Undocumented method 2 224:17
Java Inspector 089 Undocumented method 2 227:17
Java Inspector 089 Undocumented method 2 230:17
Java Inspector 089 Undocumented method 2 233:17
Java Inspector 089 Undocumented method 2 236:17
Java Inspector 089 Undocumented method 2 239:17
Java Inspector 089 Undocumented method 2 242:17
Java Inspector 089 Undocumented method 2 245:17
Java Inspector 089 Undocumented method 2 248:17
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 69:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22*/
23
24package biz.hammurapi.sql;
25
26import java.io.PrintWriter;
27import java.sql.CallableStatement;
28import java.sql.Connection;
29import java.sql.DatabaseMetaData;
30import java.sql.PreparedStatement;
31import java.sql.SQLException;
32import java.sql.SQLWarning;
33import java.sql.Savepoint;
34import java.sql.Statement;
35import java.util.ArrayList;
36import java.util.Collection;
37import java.util.HashMap;
38import java.util.Map;
39
40import javax.sql.DataSource;
41
42/**
43 * Wraps another datasource for re-using already allocated thread connections.
44 * Maintains one connection per thread/user name/pwd. Connection is allocated on first
45 * getConnection() call. Every next call increments use counter.
46 * Connection closes when it is not used (counter==0).
47 *
48 * @author Pavel Vlasov
49 */
50public class ConnectionPerThreadDataSourceFilter implements DataSource {
51
52 // Key for connection without user name and password.
53 private Object key = new Object();
54
55 private ThreadLocal connectionTL=new ThreadLocal() {
56 protected Object initialValue() {
57 return new HashMap();
58 }
59 };
60
61 private DataSource master;
62
63 /**
64 * Constructor
65 * @param master Master data source
66 * @param initConnectionTransaction optional transaction to initialize connection
67 * e.g. set database schema.
68 */
69 public ConnectionPerThreadDataSourceFilter(DataSource master) {
70 this.master = master;
71 }
72
73 public int getLoginTimeout() throws SQLException {
74 return master.getLoginTimeout();
75 }
76
77 public void setLoginTimeout(int seconds) throws SQLException {
78 master.setLoginTimeout(seconds);
79 }
80
81 public PrintWriter getLogWriter() throws SQLException {
82 return master.getLogWriter();
83 }
84
85 public void setLogWriter(PrintWriter out) throws SQLException {
86 master.setLogWriter(out);
87 }
88
89 public Connection getConnection() throws SQLException {
90 Map threadMap = (Map) connectionTL.get();
91 Connection ret = (Connection) threadMap.get(key);
92 if (ret==null) {
93 ret = new ConnectionWrapper(master.getConnection(), key, threadMap);
94 }
95 return ret;
96 }
97
98 public Connection getConnection(String user, final String password) throws SQLException {
99 Collection key = new ArrayList();
100 key.add(user);
101 key.add(password);
102 Map threadMap = (Map) connectionTL.get();
103 Connection ret = (Connection) threadMap.get(key);
104 if (ret==null) {
105 ret = new ConnectionWrapper(master.getConnection(), key, threadMap);
106 }
107 return ret;
108 }
109
110 private class ConnectionWrapper implements Connection {
111 Connection master;
112
113 private ConnectionWrapper(Connection master, Object key, Map threadMap) {
114 super();
115 this.master = master;
116 this.key = key;
117 this.threadMap = threadMap;
118 }
119
120 public void clearWarnings() throws SQLException {
121 master.clearWarnings();
122 }
123 public void close() throws SQLException {
124 counter--;
125 if (counter==0) {
126 master.close();
127 master = null;
128 threadMap.remove(key);
129 }
130 }
131 public void commit() throws SQLException {
132 master.commit();
133 }
134 public Statement createStatement() throws SQLException {
135 return master.createStatement();
136 }
137 public Statement createStatement(int resultSetType,
138 int resultSetConcurrency, int resultSetHoldability)
139 throws SQLException {
140 return master.createStatement(resultSetType, resultSetConcurrency,
141 resultSetHoldability);
142 }
143 public Statement createStatement(int resultSetType,
144 int resultSetConcurrency) throws SQLException {
145 return master.createStatement(resultSetType, resultSetConcurrency);
146 }
147 public boolean getAutoCommit() throws SQLException {
148 return master.getAutoCommit();
149 }
150 public String getCatalog() throws SQLException {
151 return master.getCatalog();
152 }
153 public int getHoldability() throws SQLException {
154 return master.getHoldability();
155 }
156 public DatabaseMetaData getMetaData() throws SQLException {
157 return master.getMetaData();
158 }
159 public int getTransactionIsolation() throws SQLException {
160 return master.getTransactionIsolation();
161 }
162 public Map getTypeMap() throws SQLException {
163 return master.getTypeMap();
164 }
165 public SQLWarning getWarnings() throws SQLException {
166 return master.getWarnings();
167 }
168 public boolean isClosed() throws SQLException {
169 return counter==0 || (master!=null && master.isClosed());
170 }
171 public boolean isReadOnly() throws SQLException {
172 return master.isReadOnly();
173 }
174 public String nativeSQL(String sql) throws SQLException {
175 return master.nativeSQL(sql);
176 }
177 public CallableStatement prepareCall(String sql, int resultSetType,
178 int resultSetConcurrency, int resultSetHoldability)
179 throws SQLException {
180 return master.prepareCall(sql, resultSetType, resultSetConcurrency,
181 resultSetHoldability);
182 }
183 public CallableStatement prepareCall(String sql, int resultSetType,
184 int resultSetConcurrency) throws SQLException {
185 return master.prepareCall(sql, resultSetType, resultSetConcurrency);
186 }
187 public CallableStatement prepareCall(String sql) throws SQLException {
188 return master.prepareCall(sql);
189 }
190 public PreparedStatement prepareStatement(String sql,
191 int resultSetType, int resultSetConcurrency,
192 int resultSetHoldability) throws SQLException {
193 return master.prepareStatement(sql, resultSetType,
194 resultSetConcurrency, resultSetHoldability);
195 }
196 public PreparedStatement prepareStatement(String sql,
197 int resultSetType, int resultSetConcurrency)
198 throws SQLException {
199 return master.prepareStatement(sql, resultSetType,
200 resultSetConcurrency);
201 }
202 public PreparedStatement prepareStatement(String sql,
203 int autoGeneratedKeys) throws SQLException {
204 return master.prepareStatement(sql, autoGeneratedKeys);
205 }
206 public PreparedStatement prepareStatement(String sql,
207 int[] columnIndexes) throws SQLException {
208 return master.prepareStatement(sql, columnIndexes);
209 }
210 public PreparedStatement prepareStatement(String sql,
211 String[] columnNames) throws SQLException {
212 return master.prepareStatement(sql, columnNames);
213 }
214 public PreparedStatement prepareStatement(String sql)
215 throws SQLException {
216 return master.prepareStatement(sql);
217 }
218 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
219 master.releaseSavepoint(savepoint);
220 }
221 public void rollback() throws SQLException {
222 master.rollback();
223 }
224 public void rollback(Savepoint savepoint) throws SQLException {
225 master.rollback(savepoint);
226 }
227 public void setAutoCommit(boolean autoCommit) throws SQLException {
228 master.setAutoCommit(autoCommit);
229 }
230 public void setCatalog(String catalog) throws SQLException {
231 master.setCatalog(catalog);
232 }
233 public void setHoldability(int holdability) throws SQLException {
234 master.setHoldability(holdability);
235 }
236 public void setReadOnly(boolean readOnly) throws SQLException {
237 master.setReadOnly(readOnly);
238 }
239 public Savepoint setSavepoint() throws SQLException {
240 return master.setSavepoint();
241 }
242 public Savepoint setSavepoint(String name) throws SQLException {
243 return master.setSavepoint(name);
244 }
245 public void setTransactionIsolation(int level) throws SQLException {
246 master.setTransactionIsolation(level);
247 }
248 public void setTypeMap(Map arg0) throws SQLException {
249 master.setTypeMap(arg0);
250 }
251
252 int counter=1;
253 Object key;
254 Map threadMap;
255 }
256
257}
258
259