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.h2 / src / main / java / org / gvsig / fmap / dal / store / h2 / H2Resource.java @ 41719
History | View | Annotate | Download (6.82 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.h2; |
29 |
|
30 |
import java.sql.SQLException; |
31 |
import java.text.MessageFormat; |
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.exception.AccessResourceException; |
39 |
import org.gvsig.fmap.dal.store.jdbc.JDBCResource; |
40 |
import org.gvsig.fmap.dal.store.jdbc.JDBCResourceParameters; |
41 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
42 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException; |
43 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
public class H2Resource extends JDBCResource { |
48 |
|
49 |
final static private Logger logger = LoggerFactory |
50 |
.getLogger(H2Resource.class); |
51 |
|
52 |
public final static String NAME = "H2SQLResource"; |
53 |
public static final String DESCRIPTION = "H2SQL Connection"; |
54 |
|
55 |
public H2Resource(H2ResourceParameters parameters)
|
56 |
throws InitializeException {
|
57 |
super(parameters);
|
58 |
} |
59 |
|
60 |
public String getName() throws AccessResourceException { |
61 |
H2ResourceParameters params = (H2ResourceParameters) this.getParameters();
|
62 |
return MessageFormat.format("H2SQLResource({0},{1})", |
63 |
new Object[] { params.getUrl(),params.getUser() }); |
64 |
} |
65 |
|
66 |
public String toString() { |
67 |
try {
|
68 |
return this.getName(); |
69 |
} catch(Exception ex) { |
70 |
return super.toString(); |
71 |
} |
72 |
} |
73 |
|
74 |
protected void connectToDB() throws DataException { |
75 |
if (this.dataSource != null) { |
76 |
return;
|
77 |
} |
78 |
JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
79 |
.getParameters(); |
80 |
BasicDataSource dataSource = new BasicDataSource();
|
81 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
82 |
dataSource.setUsername(jdbcParams.getUser()); |
83 |
dataSource.setPassword(jdbcParams.getPassword()); |
84 |
dataSource.setUrl(jdbcParams.getUrl()); |
85 |
|
86 |
dataSource.setMaxWait(60L * 1000); // FIXME |
87 |
|
88 |
// FIXME Set Pool parameters:
|
89 |
/*
|
90 |
dataSource.setMaxActive(maxActive);
|
91 |
dataSource.setMaxIdle(maxActive);
|
92 |
dataSource.setMaxOpenPreparedStatements(maxActive);
|
93 |
dataSource.setMaxWait(maxActive);
|
94 |
dataSource.setInitialSize(initialSize);
|
95 |
dataSource.setDefaultReadOnly(defaultReadOnly);
|
96 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
97 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
98 |
dataSource.setMinIdle(minIdle);
|
99 |
dataSource.setTestOnBorrow(testOnBorrow);
|
100 |
dataSource.setTestOnReturn(testOnReturn);
|
101 |
dataSource.setTestWhileIdle(testOnReturn);
|
102 |
dataSource
|
103 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
104 |
|
105 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
106 |
dataSource.setLoginTimeout(seconds);
|
107 |
dataSource.setLogWriter(out);
|
108 |
*/
|
109 |
|
110 |
this.dataSource = dataSource;
|
111 |
} |
112 |
protected void registerJDBCDriver() throws InitializeException { |
113 |
String className = ((JDBCResourceParameters) getParameters())
|
114 |
.getJDBCDriverClassName(); |
115 |
if (className == null) { |
116 |
return;
|
117 |
} |
118 |
|
119 |
Class theClass = null; |
120 |
try {
|
121 |
theClass = Class.forName(className);
|
122 |
} catch (Exception e){ |
123 |
throw new InitializeException(e); |
124 |
} |
125 |
if (theClass == null) { |
126 |
try {
|
127 |
throw new JDBCDriverClassNotFoundException(this.getName(), |
128 |
className); |
129 |
} catch (AccessResourceException e) {
|
130 |
throw new InitializeException(e); |
131 |
|
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
protected DataSource createDataSource() { |
137 |
H2ResourceParameters jdbcParams = (H2ResourceParameters) this
|
138 |
.getParameters(); |
139 |
BasicDataSource dataSource = new BasicDataSource();
|
140 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
141 |
dataSource.setUsername(jdbcParams.getUser()); |
142 |
dataSource.setPassword(jdbcParams.getPassword()); |
143 |
dataSource.setUrl(jdbcParams.getUrl()); |
144 |
|
145 |
dataSource.setMaxWait(60L * 1000); // FIXME |
146 |
|
147 |
// FIXME Set Pool parameters:
|
148 |
/*
|
149 |
dataSource.setMaxActive(maxActive);
|
150 |
dataSource.setMaxIdle(maxActive);
|
151 |
dataSource.setMaxOpenPreparedStatements(maxActive);
|
152 |
dataSource.setMaxWait(maxActive);
|
153 |
dataSource.setInitialSize(initialSize);
|
154 |
dataSource.setDefaultReadOnly(defaultReadOnly);
|
155 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
156 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
157 |
dataSource.setMinIdle(minIdle);
|
158 |
dataSource.setTestOnBorrow(testOnBorrow);
|
159 |
dataSource.setTestOnReturn(testOnReturn);
|
160 |
dataSource.setTestWhileIdle(testOnReturn);
|
161 |
dataSource
|
162 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
163 |
|
164 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
165 |
dataSource.setLoginTimeout(seconds);
|
166 |
dataSource.setLogWriter(out);
|
167 |
*/
|
168 |
return dataSource;
|
169 |
} |
170 |
|
171 |
|
172 |
public boolean isConnected() { |
173 |
if (dataSource == null) { |
174 |
return false; |
175 |
} |
176 |
if (dataSource instanceof BasicDataSource) { |
177 |
return ((BasicDataSource) dataSource).getNumActive() > 0 |
178 |
|| ((BasicDataSource) dataSource).getNumIdle() > 0;
|
179 |
} |
180 |
return true; |
181 |
} |
182 |
|
183 |
private void logPoolStatus(String src) { |
184 |
if (logger.isDebugEnabled() && dataSource instanceof BasicDataSource) { |
185 |
BasicDataSource ds = (BasicDataSource) dataSource; |
186 |
logger.debug(src + " actives:" + ds.getNumActive() + "(" |
187 |
+ ds.getMaxActive() + ") idle:" + ds.getNumIdle() + "(" |
188 |
+ ds.getMaxIdle() + ")");
|
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
private static class CanGetConnectionException extends JDBCSQLException { |
194 |
public CanGetConnectionException(String datasource, SQLException cause) { |
195 |
super("Can't get a connection to the data source (%(datasource))", cause, "_CanGetConnectionException", 0); |
196 |
setValue("datasource", datasource);
|
197 |
} |
198 |
} |
199 |
|
200 |
protected synchronized Object getTheConnection() throws DataException { |
201 |
try {
|
202 |
Object conn = this.dataSource.getConnection(); |
203 |
logPoolStatus("getTheConnection");
|
204 |
return conn;
|
205 |
} catch (SQLException e) { |
206 |
throw new CanGetConnectionException(this.toString(),e); |
207 |
} |
208 |
} |
209 |
|
210 |
} |