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