svn-gvsig-desktop / 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 @ 41880
History | View | Annotate | Download (8.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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.
|
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.
|
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.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.fmap.dal.store.jdbc; |
26 |
|
27 |
import java.sql.Connection; |
28 |
import java.sql.SQLException; |
29 |
import java.text.MessageFormat; |
30 |
|
31 |
import javax.sql.DataSource; |
32 |
|
33 |
import org.apache.commons.dbcp.BasicDataSource; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.exception.InitializeException; |
36 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
37 |
import org.gvsig.fmap.dal.resource.db.AbstractDBResourceNoBlocker; |
38 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
39 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
40 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
41 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
42 |
import org.slf4j.Logger; |
43 |
import org.slf4j.LoggerFactory; |
44 |
|
45 |
public class JDBCResource extends AbstractDBResourceNoBlocker { |
46 |
|
47 |
final static private Logger logger = LoggerFactory |
48 |
.getLogger(JDBCResource.class); |
49 |
|
50 |
public final static String NAME = "JDBCResource"; |
51 |
public static final String DESCRIPTION = "JDBC Connection"; |
52 |
|
53 |
protected DataSource dataSource = null; |
54 |
|
55 |
public JDBCResource(JDBCResourceParameters parameters)
|
56 |
throws InitializeException {
|
57 |
super(parameters);
|
58 |
registerJDBCDriver(); |
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 |
} |
110 |
} |
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;
|
127 |
} |
128 |
|
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 |
} |
138 |
|
139 |
public boolean isThis(ResourceParameters parameters) |
140 |
throws ResourceException {
|
141 |
if (!super.isThis(parameters)) { |
142 |
return false; |
143 |
} |
144 |
|
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 |
} |
152 |
|
153 |
} |
154 |
|
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; |
165 |
|
166 |
} |
167 |
|
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 |
} |
178 |
|
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()); |
187 |
|
188 |
|
189 |
dataSource.setMaxWait(60L * 1000); // FIXME |
190 |
|
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);
|
207 |
|
208 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
209 |
dataSource.setLoginTimeout(seconds);
|
210 |
dataSource.setLogWriter(out);
|
211 |
*/
|
212 |
return dataSource;
|
213 |
} |
214 |
|
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()); |
226 |
|
227 |
dataSource.setMaxWait(60L * 1000); // FIXME |
228 |
|
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);
|
245 |
|
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 |
} |
261 |
} |