root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / jdbc / JDBCResource.java @ 27672
History | View | Annotate | Download (5.11 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.store.jdbc; |
29 |
|
30 |
import java.sql.Connection; |
31 |
import java.sql.SQLException; |
32 |
|
33 |
import javax.sql.DataSource; |
34 |
|
35 |
import org.apache.commons.dbcp.BasicDataSource; |
36 |
import org.gvsig.fmap.dal.exception.DataException; |
37 |
import org.gvsig.fmap.dal.exception.InitializeException; |
38 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
39 |
import org.gvsig.fmap.dal.resource.db.DBResource; |
40 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
41 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
42 |
import org.slf4j.Logger; |
43 |
import org.slf4j.LoggerFactory; |
44 |
|
45 |
public abstract class JDBCResource extends DBResource { |
46 |
|
47 |
final static private Logger logger = LoggerFactory |
48 |
.getLogger(JDBCResource.class); |
49 |
|
50 |
|
51 |
protected DataSource dataSource = null; |
52 |
|
53 |
protected JDBCResource(JDBCResourceParameters parameters)
|
54 |
throws InitializeException {
|
55 |
super(parameters);
|
56 |
registerJDBCDriver(); |
57 |
|
58 |
} |
59 |
|
60 |
protected void registerJDBCDriver() throws InitializeException { |
61 |
String className = ((JDBCResourceParameters) getParameters())
|
62 |
.getJDBCDriverClassName(); |
63 |
if (className == null) { |
64 |
return;
|
65 |
} |
66 |
|
67 |
try {
|
68 |
Class.forName(className);
|
69 |
} catch (Exception e){ |
70 |
throw new InitializeException(e); |
71 |
} |
72 |
} |
73 |
|
74 |
|
75 |
public Connection getJDBCConnection() throws AccessResourceException { |
76 |
return (Connection) get(); |
77 |
} |
78 |
|
79 |
private void debugPoolStatus(String src) { |
80 |
if (logger.isDebugEnabled() && dataSource instanceof BasicDataSource) { |
81 |
BasicDataSource ds = (BasicDataSource) dataSource; |
82 |
logger.debug(src + " actives:" + ds.getNumActive() + "(" |
83 |
+ ds.getMaxActive() + ") idle:" + ds.getNumIdle() + "(" |
84 |
+ ds.getMaxIdle() + ")");
|
85 |
} |
86 |
|
87 |
} |
88 |
|
89 |
protected synchronized Object getTheConnection() throws DataException { |
90 |
try {
|
91 |
Object conn = this.dataSource.getConnection(); |
92 |
debugPoolStatus("getTheConnection");
|
93 |
return conn;
|
94 |
} catch (SQLException e) { |
95 |
throw new JDBCSQLException(e); |
96 |
} |
97 |
} |
98 |
|
99 |
public boolean isThis(ResourceParameters parameters) |
100 |
throws ResourceException {
|
101 |
if (!super.isThis(parameters)) { |
102 |
return false; |
103 |
} |
104 |
|
105 |
String dbName = ((JDBCResourceParameters) parameters).getDBName();
|
106 |
String myDbName = ((JDBCResourceParameters) getParameters())
|
107 |
.getDBName(); |
108 |
if (dbName != myDbName) {
|
109 |
if (!(dbName != null && dbName.equals(myDbName))) { |
110 |
return false; |
111 |
} |
112 |
|
113 |
} |
114 |
|
115 |
String driver = ((JDBCResourceParameters) parameters)
|
116 |
.getJDBCDriverClassName(); |
117 |
String myDriver = ((JDBCResourceParameters) getParameters())
|
118 |
.getJDBCDriverClassName(); |
119 |
if (driver != myDriver) {
|
120 |
if (!(driver != null && driver.equals(myDriver))) { |
121 |
return false; |
122 |
} |
123 |
} |
124 |
return true; |
125 |
|
126 |
} |
127 |
|
128 |
public boolean isConnected() { |
129 |
if (dataSource == null) { |
130 |
return false; |
131 |
} |
132 |
if (dataSource instanceof BasicDataSource) { |
133 |
return ((BasicDataSource) dataSource).getNumActive() > 0 |
134 |
|| ((BasicDataSource) dataSource).getNumIdle() > 0;
|
135 |
} |
136 |
return true; |
137 |
} |
138 |
|
139 |
|
140 |
protected void connectToDB() throws DataException { |
141 |
if (this.dataSource != null) { |
142 |
return;
|
143 |
} |
144 |
JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
145 |
.getParameters(); |
146 |
BasicDataSource dataSource = new BasicDataSource();
|
147 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
148 |
dataSource.setUsername(jdbcParams.getUser()); |
149 |
dataSource.setPassword(jdbcParams.getPassword()); |
150 |
dataSource.setUrl(jdbcParams.getUrl()); |
151 |
|
152 |
dataSource.setMaxWait(60L * 1000); // FIXME |
153 |
|
154 |
// FIXME Set Pool parameters:
|
155 |
/*
|
156 |
dataSource.setMaxActive(maxActive);
|
157 |
dataSource.setMaxIdle(maxActive);
|
158 |
dataSource.setMaxOpenPreparedStatements(maxActive);
|
159 |
dataSource.setMaxWait(maxActive);
|
160 |
dataSource.setInitialSize(initialSize);
|
161 |
dataSource.setDefaultReadOnly(defaultReadOnly);
|
162 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
163 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
164 |
dataSource.setMinIdle(minIdle);
|
165 |
dataSource.setTestOnBorrow(testOnBorrow);
|
166 |
dataSource.setTestOnReturn(testOnReturn);
|
167 |
dataSource.setTestWhileIdle(testOnReturn);
|
168 |
dataSource
|
169 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
170 |
|
171 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
172 |
dataSource.setLoginTimeout(seconds);
|
173 |
dataSource.setLogWriter(out);
|
174 |
*/
|
175 |
|
176 |
this.dataSource = dataSource;
|
177 |
} |
178 |
|
179 |
|
180 |
} |