Revision 43020 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc/JDBCResource.java
JDBCResource.java | ||
---|---|---|
3 | 3 |
* |
4 | 4 |
* Copyright (C) 2007-2013 gvSIG Association. |
5 | 5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License
|
|
8 |
* as published by the Free Software Foundation; either version 3
|
|
9 |
* of the License, or (at your option) any later version.
|
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
|
9 |
* version. |
|
10 | 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
|
|
14 |
* GNU General Public License for more details.
|
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
14 |
* details. |
|
15 | 15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
16 |
* You should have received a copy of the GNU General Public License along with |
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51 |
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
20 | 19 |
* |
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
23 | 22 |
*/ |
24 |
|
|
25 | 23 |
package org.gvsig.fmap.dal.store.jdbc; |
26 | 24 |
|
27 | 25 |
import java.sql.Connection; |
... | ... | |
31 | 29 |
import javax.sql.DataSource; |
32 | 30 |
|
33 | 31 |
import org.apache.commons.dbcp.BasicDataSource; |
32 |
import org.apache.commons.lang3.StringUtils; |
|
34 | 33 |
import org.gvsig.fmap.dal.exception.DataException; |
35 | 34 |
import org.gvsig.fmap.dal.exception.InitializeException; |
36 | 35 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
... | ... | |
44 | 43 |
|
45 | 44 |
public class JDBCResource extends AbstractDBResourceNoBlocker { |
46 | 45 |
|
47 |
final static private Logger logger = LoggerFactory |
|
48 |
.getLogger(JDBCResource.class); |
|
46 |
final static private Logger logger = LoggerFactory.getLogger(JDBCResource.class); |
|
49 | 47 |
|
50 |
public final static String NAME = "JDBCResource";
|
|
51 |
public static final String DESCRIPTION = "JDBC Connection";
|
|
48 |
public final static String NAME = "JDBCResource";
|
|
49 |
public static final String DESCRIPTION = "JDBC Connection";
|
|
52 | 50 |
|
53 |
protected DataSource dataSource = null;
|
|
51 |
protected DataSource dataSource = null;
|
|
54 | 52 |
|
55 |
public JDBCResource(JDBCResourceParameters parameters) |
|
56 |
throws InitializeException { |
|
57 |
super(parameters); |
|
58 |
registerJDBCDriver(); |
|
53 |
public JDBCResource( |
|
54 |
JDBCResourceParameters parameters |
|
55 |
) throws InitializeException { |
|
56 |
super(parameters); |
|
57 |
registerJDBCDriver(); |
|
58 |
} |
|
59 | 59 |
|
60 |
} |
|
61 |
|
|
62 |
public String getName() throws AccessResourceException { |
|
63 |
JDBCResourceParameters params = (JDBCResourceParameters) this |
|
64 |
.getParameters(); |
|
65 |
return MessageFormat.format("JDBCResource({0},{1},{2},{3},{4})", |
|
66 |
new Object[] { |
|
67 |
params.getJDBCDriverClassName(), |
|
68 |
params.getHost(), params.getPort(), |
|
69 |
params.getUser(), params.getDBName() }); |
|
70 |
} |
|
71 |
|
|
72 |
@SuppressWarnings("unchecked") |
|
73 |
protected void registerJDBCDriver() throws InitializeException { |
|
74 |
String className = ((JDBCResourceParameters) getParameters()) |
|
75 |
.getJDBCDriverClassName(); |
|
76 |
if (className == null) { |
|
77 |
return; |
|
78 |
} |
|
79 |
|
|
80 |
Class theClass = null; |
|
81 |
try { |
|
82 |
theClass = Class.forName(className); |
|
83 |
} catch (Exception e){ |
|
84 |
throw new InitializeException(e); |
|
85 |
} |
|
86 |
if (theClass == null) { |
|
87 |
try { |
|
88 |
throw new JDBCDriverClassNotFoundException(this.getName(), |
|
89 |
className); |
|
90 |
} catch (AccessResourceException e) { |
|
91 |
throw new InitializeException(e); |
|
92 |
|
|
93 |
} |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
|
|
98 |
public Connection getJDBCConnection() throws AccessResourceException { |
|
99 |
return (Connection) get(); |
|
100 |
} |
|
101 |
|
|
102 |
public void closeConnection(Connection connection) { |
|
103 |
try { |
|
104 |
logger.debug("before-close connection"+ this.getStatusInformation()); |
|
105 |
connection.close(); |
|
106 |
logger.debug("after close connection "+ this.getStatusInformation()); |
|
107 |
} catch (Exception ex) { |
|
108 |
logger.warn("Problems closing connection.",ex); |
|
109 |
} |
|
60 |
/** |
|
61 |
* Registra/Carga el driver de JDBC. |
|
62 |
* |
|
63 |
* Debe ser sobreescrita en cada proveedor de datos para asegurar que se |
|
64 |
* tegna acceso al driver. |
|
65 |
* |
|
66 |
* @throws InitializeException |
|
67 |
*/ |
|
68 |
protected void registerJDBCDriver() throws InitializeException { |
|
69 |
String className = this.getParameters().getJDBCDriverClassName(); |
|
70 |
if (className == null) { |
|
71 |
return; |
|
110 | 72 |
} |
111 |
|
|
112 |
private void debugPoolStatus(String src) { |
|
113 |
if (logger.isDebugEnabled() ) { |
|
114 |
logger.debug(src + " " + this.getStatusInformation()); |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
public String getStatusInformation() { |
|
119 |
if ( !(dataSource instanceof BasicDataSource) ) { |
|
120 |
return "Connected: " + this.isConnected(); |
|
121 |
} |
|
122 |
BasicDataSource ds = (BasicDataSource) dataSource; |
|
123 |
String s = "Connected: " + this.isConnected() + ", " + |
|
124 |
"actives: "+ ds.getNumActive() + "/"+ ds.getMaxActive() + ", " + |
|
125 |
"idle: " + ds.getNumIdle() + "/"+ ds.getMaxIdle() ; |
|
126 |
return s; |
|
73 |
try { |
|
74 |
Class theClass = Class.forName(className); |
|
75 |
if (theClass == null) { |
|
76 |
throw new JDBCDriverClassNotFoundException(this.getName(), className); |
|
77 |
} |
|
78 |
} catch (Exception e) { |
|
79 |
throw new InitializeException(e); |
|
127 | 80 |
} |
81 |
} |
|
128 | 82 |
|
129 |
protected synchronized Object getTheConnection() throws DataException { |
|
130 |
try { |
|
131 |
Object conn = this.dataSource.getConnection(); |
|
132 |
debugPoolStatus("getTheConnection"); |
|
133 |
return conn; |
|
134 |
} catch (SQLException e) { |
|
135 |
throw new JDBCSQLException(e); |
|
136 |
} |
|
137 |
} |
|
83 |
/** |
|
84 |
* Crea el JDBC DataSource a partir de la informacion de los parametros |
|
85 |
* del recurso. |
|
86 |
* |
|
87 |
* Debe ser sobreescrita en cada proveedor de datos para asegurar que se |
|
88 |
* tenga acceso al driver. |
|
89 |
* |
|
90 |
* @return |
|
91 |
*/ |
|
92 |
protected DataSource createDataSource() { |
|
93 |
JDBCResourceParameters jdbcParams = this.getParameters(); |
|
94 |
BasicDataSource dataSource = new BasicDataSource(); |
|
95 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
|
96 |
dataSource.setUsername(jdbcParams.getUser()); |
|
97 |
dataSource.setPassword(jdbcParams.getPassword()); |
|
98 |
dataSource.setUrl(jdbcParams.getUrl()); |
|
138 | 99 |
|
139 |
public boolean isThis(ResourceParameters parameters) |
|
140 |
throws ResourceException { |
|
141 |
if (!super.isThis(parameters)) { |
|
142 |
return false; |
|
143 |
} |
|
100 |
dataSource.setMaxWait(60L * 1000); // FIXME |
|
144 | 101 |
|
145 |
String dbName = ((JDBCResourceParameters) parameters).getDBName(); |
|
146 |
String myDbName = ((JDBCResourceParameters) getParameters()) |
|
147 |
.getDBName(); |
|
148 |
if (dbName != myDbName) { |
|
149 |
if (!(dbName != null && dbName.equals(myDbName))) { |
|
150 |
return false; |
|
151 |
} |
|
102 |
// FIXME Set Pool parameters: |
|
103 |
// dataSource.setMaxActive(maxActive); |
|
104 |
// dataSource.setMaxIdle(maxActive); |
|
105 |
// dataSource.setMaxOpenPreparedStatements(maxActive); |
|
106 |
// dataSource.setMaxWait(maxActive); |
|
107 |
// dataSource.setInitialSize(initialSize); |
|
108 |
// dataSource.setDefaultReadOnly(defaultReadOnly); |
|
109 |
// dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation); |
|
110 |
// dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); |
|
111 |
// dataSource.setMinIdle(minIdle); |
|
112 |
// dataSource.setTestOnBorrow(testOnBorrow); |
|
113 |
// dataSource.setTestOnReturn(testOnReturn); |
|
114 |
// dataSource.setTestWhileIdle(testOnReturn); |
|
115 |
// dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); |
|
116 |
// |
|
117 |
// dataSource.setAccessToUnderlyingConnectionAllowed(allow); |
|
118 |
// dataSource.setLoginTimeout(seconds); |
|
119 |
// dataSource.setLogWriter(out); |
|
120 |
return dataSource; |
|
121 |
} |
|
152 | 122 |
|
153 |
} |
|
154 | 123 |
|
155 |
String driver = ((JDBCResourceParameters) parameters) |
|
156 |
.getJDBCDriverClassName(); |
|
157 |
String myDriver = ((JDBCResourceParameters) getParameters()) |
|
158 |
.getJDBCDriverClassName(); |
|
159 |
if (driver != myDriver) { |
|
160 |
if (!(driver != null && driver.equals(myDriver))) { |
|
161 |
return false; |
|
162 |
} |
|
163 |
} |
|
164 |
return true; |
|
124 |
@Override |
|
125 |
public JDBCResourceParameters getParameters() { |
|
126 |
return (JDBCResourceParameters) super.getParameters(); |
|
127 |
} |
|
165 | 128 |
|
166 |
} |
|
129 |
@Override |
|
130 |
public String getName() throws AccessResourceException { |
|
131 |
JDBCResourceParameters params = this.getParameters(); |
|
132 |
return MessageFormat.format("JDBCResource({0},{1},{2},{3},{4})", |
|
133 |
new Object[]{ |
|
134 |
params.getJDBCDriverClassName(), |
|
135 |
params.getHost(), |
|
136 |
params.getPort(), |
|
137 |
params.getUser(), |
|
138 |
params.getDBName() |
|
139 |
} |
|
140 |
); |
|
141 |
} |
|
142 |
|
|
143 |
public Connection getJDBCConnection() throws AccessResourceException { |
|
144 |
return (Connection) get(); |
|
145 |
} |
|
167 | 146 |
|
168 |
public boolean isConnected() { |
|
169 |
if (dataSource == null) { |
|
170 |
return false; |
|
171 |
} |
|
172 |
if (dataSource instanceof BasicDataSource) { |
|
173 |
return ((BasicDataSource) dataSource).getNumActive() > 0 |
|
174 |
|| ((BasicDataSource) dataSource).getNumIdle() > 0; |
|
175 |
} |
|
176 |
return true; |
|
177 |
} |
|
147 |
public void closeConnection(Connection connection) { |
|
148 |
try { |
|
149 |
logger.debug("before-close connection" + this.getStatusInformation()); |
|
150 |
connection.close(); |
|
151 |
logger.debug("after close connection " + this.getStatusInformation()); |
|
152 |
} catch (Exception ex) { |
|
153 |
logger.warn("Problems closing connection.", ex); |
|
154 |
} |
|
155 |
} |
|
178 | 156 |
|
179 |
protected DataSource createDataSource() { |
|
180 |
JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this |
|
181 |
.getParameters(); |
|
182 |
BasicDataSource dataSource = new BasicDataSource(); |
|
183 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
|
184 |
dataSource.setUsername(jdbcParams.getUser()); |
|
185 |
dataSource.setPassword(jdbcParams.getPassword()); |
|
186 |
dataSource.setUrl(jdbcParams.getUrl()); |
|
157 |
protected void debugPoolStatus(String src) { |
|
158 |
if (logger.isDebugEnabled()) { |
|
159 |
logger.debug(src + " " + this.getStatusInformation()); |
|
160 |
} |
|
161 |
} |
|
187 | 162 |
|
163 |
public String getStatusInformation() { |
|
164 |
if (!(dataSource instanceof BasicDataSource)) { |
|
165 |
return "Connected: " + this.isConnected(); |
|
166 |
} |
|
167 |
BasicDataSource ds = (BasicDataSource) dataSource; |
|
168 |
String s = MessageFormat.format("Conneted {0}, actives {1}/{2}, idle {3}/{4}", |
|
169 |
this.isConnected(), |
|
170 |
ds.getNumActive(), |
|
171 |
ds.getMaxActive(), |
|
172 |
ds.getNumIdle(), |
|
173 |
ds.getMaxIdle() |
|
174 |
); |
|
175 |
return s; |
|
176 |
} |
|
188 | 177 |
|
189 |
dataSource.setMaxWait(60L * 1000); // FIXME |
|
178 |
@Override |
|
179 |
protected synchronized Object getTheConnection() throws DataException { |
|
180 |
try { |
|
181 |
Object conn = this.dataSource.getConnection(); |
|
182 |
debugPoolStatus("getTheConnection"); |
|
183 |
return conn; |
|
184 |
} catch (SQLException e) { |
|
185 |
throw new JDBCSQLException(e); |
|
186 |
} |
|
187 |
} |
|
190 | 188 |
|
191 |
// FIXME Set Pool parameters: |
|
192 |
/* |
|
193 |
dataSource.setMaxActive(maxActive); |
|
194 |
dataSource.setMaxIdle(maxActive); |
|
195 |
dataSource.setMaxOpenPreparedStatements(maxActive); |
|
196 |
dataSource.setMaxWait(maxActive); |
|
197 |
dataSource.setInitialSize(initialSize); |
|
198 |
dataSource.setDefaultReadOnly(defaultReadOnly); |
|
199 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation); |
|
200 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); |
|
201 |
dataSource.setMinIdle(minIdle); |
|
202 |
dataSource.setTestOnBorrow(testOnBorrow); |
|
203 |
dataSource.setTestOnReturn(testOnReturn); |
|
204 |
dataSource.setTestWhileIdle(testOnReturn); |
|
205 |
dataSource |
|
206 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); |
|
189 |
@Override |
|
190 |
public boolean isThis(ResourceParameters parameters) |
|
191 |
throws ResourceException { |
|
192 |
if (!super.isThis(parameters)) { |
|
193 |
return false; |
|
194 |
} |
|
195 |
JDBCResourceParameters params = (JDBCResourceParameters) parameters; |
|
196 |
String dbName = params.getDBName(); |
|
197 |
String myDbName = this.getParameters().getDBName(); |
|
198 |
if ( !StringUtils.equals(dbName, myDbName) ) { |
|
199 |
return false; |
|
200 |
} |
|
207 | 201 |
|
208 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow); |
|
209 |
dataSource.setLoginTimeout(seconds); |
|
210 |
dataSource.setLogWriter(out); |
|
211 |
*/ |
|
212 |
return dataSource; |
|
213 |
} |
|
202 |
String driver = params.getJDBCDriverClassName(); |
|
203 |
String myDriver = getParameters().getJDBCDriverClassName(); |
|
204 |
if ( !StringUtils.equals(driver, myDriver) ) { |
|
205 |
return false; |
|
206 |
} |
|
207 |
return true; |
|
208 |
} |
|
214 | 209 |
|
215 |
protected void connectToDB() throws DataException {
|
|
216 |
if (this.dataSource != null) {
|
|
217 |
return;
|
|
218 |
}
|
|
219 |
JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
|
220 |
.getParameters();
|
|
221 |
BasicDataSource dataSource = new BasicDataSource();
|
|
222 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
|
|
223 |
dataSource.setUsername(jdbcParams.getUser());
|
|
224 |
dataSource.setPassword(jdbcParams.getPassword());
|
|
225 |
dataSource.setUrl(jdbcParams.getUrl());
|
|
210 |
@Override
|
|
211 |
public boolean isConnected() {
|
|
212 |
if (dataSource == null) {
|
|
213 |
return false;
|
|
214 |
}
|
|
215 |
if (dataSource instanceof BasicDataSource) {
|
|
216 |
return ((BasicDataSource) dataSource).getNumActive() > 0
|
|
217 |
|| ((BasicDataSource) dataSource).getNumIdle() > 0;
|
|
218 |
}
|
|
219 |
return true;
|
|
220 |
}
|
|
226 | 221 |
|
227 |
dataSource.setMaxWait(60L * 1000); // FIXME |
|
222 |
@Override |
|
223 |
protected void connectToDB() throws DataException { |
|
224 |
if (this.dataSource != null) { |
|
225 |
return; |
|
226 |
} |
|
227 |
this.dataSource = this.createDataSource(); |
|
228 |
} |
|
228 | 229 |
|
229 |
// FIXME Set Pool parameters: |
|
230 |
/* |
|
231 |
dataSource.setMaxActive(maxActive); |
|
232 |
dataSource.setMaxIdle(maxActive); |
|
233 |
dataSource.setMaxOpenPreparedStatements(maxActive); |
|
234 |
dataSource.setMaxWait(maxActive); |
|
235 |
dataSource.setInitialSize(initialSize); |
|
236 |
dataSource.setDefaultReadOnly(defaultReadOnly); |
|
237 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation); |
|
238 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); |
|
239 |
dataSource.setMinIdle(minIdle); |
|
240 |
dataSource.setTestOnBorrow(testOnBorrow); |
|
241 |
dataSource.setTestOnReturn(testOnReturn); |
|
242 |
dataSource.setTestWhileIdle(testOnReturn); |
|
243 |
dataSource |
|
244 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); |
|
230 |
public void beginUse() { |
|
231 |
this.executeBegins(); |
|
232 |
} |
|
245 | 233 |
|
246 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow); |
|
247 |
dataSource.setLoginTimeout(seconds); |
|
248 |
dataSource.setLogWriter(out); |
|
249 |
*/ |
|
250 |
|
|
251 |
this.dataSource = dataSource; |
|
252 |
} |
|
253 |
|
|
254 |
public void beginUse() { |
|
255 |
this.executeBegins(); |
|
256 |
} |
|
257 |
|
|
258 |
public void endUse() { |
|
259 |
this.executeEnds(); |
|
260 |
} |
|
234 |
public void endUse() { |
|
235 |
this.executeEnds(); |
|
236 |
} |
|
261 | 237 |
} |
Also available in: Unified diff