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 @ 41536
History | View | Annotate | Download (7.45 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 |
private void debugPoolStatus(String src) { |
103 |
if (logger.isDebugEnabled() && dataSource instanceof BasicDataSource) { |
104 |
BasicDataSource ds = (BasicDataSource) dataSource; |
105 |
logger.debug(src + " actives:" + ds.getNumActive() + "(" |
106 |
+ ds.getMaxActive() + ") idle:" + ds.getNumIdle() + "(" |
107 |
+ ds.getMaxIdle() + ")");
|
108 |
} |
109 |
|
110 |
} |
111 |
|
112 |
protected synchronized Object getTheConnection() throws DataException { |
113 |
try {
|
114 |
Object conn = this.dataSource.getConnection(); |
115 |
debugPoolStatus("getTheConnection");
|
116 |
return conn;
|
117 |
} catch (SQLException e) { |
118 |
throw new JDBCSQLException(e); |
119 |
} |
120 |
} |
121 |
|
122 |
public boolean isThis(ResourceParameters parameters) |
123 |
throws ResourceException {
|
124 |
if (!super.isThis(parameters)) { |
125 |
return false; |
126 |
} |
127 |
|
128 |
String dbName = ((JDBCResourceParameters) parameters).getDBName();
|
129 |
String myDbName = ((JDBCResourceParameters) getParameters())
|
130 |
.getDBName(); |
131 |
if (dbName != myDbName) {
|
132 |
if (!(dbName != null && dbName.equals(myDbName))) { |
133 |
return false; |
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
String driver = ((JDBCResourceParameters) parameters)
|
139 |
.getJDBCDriverClassName(); |
140 |
String myDriver = ((JDBCResourceParameters) getParameters())
|
141 |
.getJDBCDriverClassName(); |
142 |
if (driver != myDriver) {
|
143 |
if (!(driver != null && driver.equals(myDriver))) { |
144 |
return false; |
145 |
} |
146 |
} |
147 |
return true; |
148 |
|
149 |
} |
150 |
|
151 |
public boolean isConnected() { |
152 |
if (dataSource == null) { |
153 |
return false; |
154 |
} |
155 |
if (dataSource instanceof BasicDataSource) { |
156 |
return ((BasicDataSource) dataSource).getNumActive() > 0 |
157 |
|| ((BasicDataSource) dataSource).getNumIdle() > 0;
|
158 |
} |
159 |
return true; |
160 |
} |
161 |
|
162 |
protected DataSource createDataSource() { |
163 |
JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
164 |
.getParameters(); |
165 |
BasicDataSource dataSource = new BasicDataSource();
|
166 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
167 |
dataSource.setUsername(jdbcParams.getUser()); |
168 |
dataSource.setPassword(jdbcParams.getPassword()); |
169 |
dataSource.setUrl(jdbcParams.getUrl()); |
170 |
|
171 |
|
172 |
dataSource.setMaxWait(60L * 1000); // FIXME |
173 |
|
174 |
// FIXME Set Pool parameters:
|
175 |
/*
|
176 |
dataSource.setMaxActive(maxActive);
|
177 |
dataSource.setMaxIdle(maxActive);
|
178 |
dataSource.setMaxOpenPreparedStatements(maxActive);
|
179 |
dataSource.setMaxWait(maxActive);
|
180 |
dataSource.setInitialSize(initialSize);
|
181 |
dataSource.setDefaultReadOnly(defaultReadOnly);
|
182 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
183 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
184 |
dataSource.setMinIdle(minIdle);
|
185 |
dataSource.setTestOnBorrow(testOnBorrow);
|
186 |
dataSource.setTestOnReturn(testOnReturn);
|
187 |
dataSource.setTestWhileIdle(testOnReturn);
|
188 |
dataSource
|
189 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
190 |
|
191 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
192 |
dataSource.setLoginTimeout(seconds);
|
193 |
dataSource.setLogWriter(out);
|
194 |
*/
|
195 |
return dataSource;
|
196 |
} |
197 |
|
198 |
protected void connectToDB() throws DataException { |
199 |
if (this.dataSource != null) { |
200 |
return;
|
201 |
} |
202 |
JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
203 |
.getParameters(); |
204 |
BasicDataSource dataSource = new BasicDataSource();
|
205 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
206 |
dataSource.setUsername(jdbcParams.getUser()); |
207 |
dataSource.setPassword(jdbcParams.getPassword()); |
208 |
dataSource.setUrl(jdbcParams.getUrl()); |
209 |
|
210 |
dataSource.setMaxWait(60L * 1000); // FIXME |
211 |
|
212 |
// FIXME Set Pool parameters:
|
213 |
/*
|
214 |
dataSource.setMaxActive(maxActive);
|
215 |
dataSource.setMaxIdle(maxActive);
|
216 |
dataSource.setMaxOpenPreparedStatements(maxActive);
|
217 |
dataSource.setMaxWait(maxActive);
|
218 |
dataSource.setInitialSize(initialSize);
|
219 |
dataSource.setDefaultReadOnly(defaultReadOnly);
|
220 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
221 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
222 |
dataSource.setMinIdle(minIdle);
|
223 |
dataSource.setTestOnBorrow(testOnBorrow);
|
224 |
dataSource.setTestOnReturn(testOnReturn);
|
225 |
dataSource.setTestWhileIdle(testOnReturn);
|
226 |
dataSource
|
227 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
228 |
|
229 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
230 |
dataSource.setLoginTimeout(seconds);
|
231 |
dataSource.setLogWriter(out);
|
232 |
*/
|
233 |
|
234 |
this.dataSource = dataSource;
|
235 |
} |
236 |
|
237 |
public void beginUse() { |
238 |
this.executeBegins();
|
239 |
} |
240 |
|
241 |
public void endUse() { |
242 |
this.executeEnds();
|
243 |
} |
244 |
} |