Revision 922
trunk/org.gvsig.postgresql/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLHelper.java | ||
---|---|---|
1 | 1 |
|
2 | 2 |
package org.gvsig.postgresql.dal; |
3 | 3 |
|
4 |
import java.io.IOException; |
|
5 | 4 |
import java.sql.Connection; |
5 |
import java.sql.DriverManager; |
|
6 | 6 |
import java.sql.SQLException; |
7 | 7 |
import org.apache.commons.dbcp2.BasicDataSource; |
8 | 8 |
import org.apache.commons.lang3.BooleanUtils; |
... | ... | |
24 | 24 |
import org.gvsig.postgresql.dal.operations.PostgreSQLOperationsFactory; |
25 | 25 |
import org.gvsig.tools.dispose.Disposable; |
26 | 26 |
import org.gvsig.tools.exception.BaseException; |
27 |
import org.postgresql.util.DriverInfo; |
|
27 | 28 |
import org.slf4j.Logger; |
28 | 29 |
import org.slf4j.LoggerFactory; |
29 | 30 |
|
31 |
@SuppressWarnings("UseSpecificCatch") |
|
30 | 32 |
public class PostgreSQLHelper extends JDBCHelperBase { |
31 | 33 |
|
32 | 34 |
/* friend */ static final Logger LOGGER = LoggerFactory.getLogger(PostgreSQLHelper.class); |
33 | 35 |
|
34 | 36 |
public static final String POSTGRESQL_JDBC_DRIVER = "org.postgresql.Driver"; |
35 | 37 |
|
38 |
public static final boolean USE_CONNECTIONS_POOL = true; |
|
39 |
|
|
36 | 40 |
public static String getConnectionURL(PostgreSQLConnectionParameters params) { |
37 | 41 |
return getConnectionURL( |
38 | 42 |
params.getHost(), |
... | ... | |
66 | 70 |
this.connectionParameters = connectionParameters; |
67 | 71 |
} |
68 | 72 |
|
73 |
@Override |
|
69 | 74 |
public Connection getConnection() throws SQLException { |
70 |
if (this.dataSource == null) { |
|
71 |
this.dataSource = this.createDataSource(); |
|
72 |
} |
|
73 |
if( LOGGER.isDebugEnabled() ) { |
|
74 |
LOGGER.debug("getConnection:\n" + getStatusInformation()); |
|
75 |
} |
|
75 |
|
|
76 | 76 |
Connection conn; |
77 |
try { |
|
78 |
conn = this.dataSource.getConnection(); |
|
79 |
} catch(Throwable ex) { |
|
80 |
LOGGER.debug("Error getting connection from pool.",ex); |
|
81 |
throw ex; |
|
77 |
if( USE_CONNECTIONS_POOL ) { |
|
78 |
if (this.dataSource == null) { |
|
79 |
this.dataSource = this.createDataSource(); |
|
80 |
} |
|
81 |
if( LOGGER.isDebugEnabled() ) { |
|
82 |
LOGGER.debug("getConnection:\n" + getStatusInformation()); |
|
83 |
} |
|
84 |
try { |
|
85 |
conn = this.dataSource.getConnection(); |
|
86 |
} catch(Throwable ex) { |
|
87 |
LOGGER.debug("Error getting connection from pool.",ex); |
|
88 |
throw ex; |
|
89 |
} |
|
90 |
try { |
|
91 |
conn.setNetworkTimeout(null, this.connectionParameters.getNetworkTimeout()); |
|
92 |
} catch(Throwable ex) { |
|
93 |
LOGGER.warn("Error setting the network timeout.",ex); |
|
94 |
} |
|
95 |
if( LOGGER.isDebugEnabled() ) { |
|
96 |
LOGGER.debug("Created connection: {}\n NumActive: {}\n NumIdle: {}", |
|
97 |
new Object[] { |
|
98 |
conn.hashCode(), |
|
99 |
this.dataSource.getNumActive(), |
|
100 |
this.dataSource.getNumIdle() |
|
101 |
} |
|
102 |
); |
|
103 |
} |
|
104 |
} else { |
|
105 |
try { |
|
106 |
conn = DriverManager.getConnection( |
|
107 |
connectionParameters.getUrl(), |
|
108 |
connectionParameters.getUser(), |
|
109 |
connectionParameters.getPassword() |
|
110 |
); |
|
111 |
} catch(Throwable th) { |
|
112 |
throw th; |
|
113 |
} |
|
114 |
if( LOGGER.isDebugEnabled() ) { |
|
115 |
LOGGER.debug("Created not polled connection: {}", |
|
116 |
new Object[] { |
|
117 |
conn.hashCode() |
|
118 |
} |
|
119 |
); |
|
120 |
} |
|
82 | 121 |
} |
83 |
try { |
|
84 |
conn.setNetworkTimeout(null, this.connectionParameters.getNetworkTimeout()); |
|
85 |
} catch(Throwable ex) { |
|
86 |
LOGGER.warn("Error setting the network timeout.",ex); |
|
87 |
} |
|
88 |
if( LOGGER.isDebugEnabled() ) { |
|
89 |
LOGGER.debug("Created connection: {}\n NumActive: {}\n NumIdle: {}", |
|
90 |
new Object[] { |
|
91 |
conn.hashCode(), |
|
92 |
this.dataSource.getNumActive(), |
|
93 |
this.dataSource.getNumIdle() |
|
94 |
} |
|
95 |
); |
|
96 |
} |
|
122 |
LOGGER.debug("PostgreSQL JDBC Driver: "+DriverInfo.DRIVER_VERSION); |
|
97 | 123 |
return conn; |
98 | 124 |
} |
99 | 125 |
|
100 | 126 |
public String getStatusInformation() { |
101 | 127 |
StringBuilder builder = new StringBuilder(); |
102 |
builder.append("BasicDataSource pool status:\n"); |
|
103 |
builder.append(" Connection URL: '").append(this.dataSource.getUrl()).append("'\n"); |
|
104 |
if( this.dataSource.getInitialSize()>0 ) { |
|
105 |
builder.append(" InitialSize: ").append(this.dataSource.getInitialSize()).append(" (The initial number of connections that are created when the pool is started)\n"); |
|
128 |
if( this.dataSource==null ) { |
|
129 |
builder.append("Not poolled connection:\n"); |
|
130 |
builder.append(" Connection URL: '").append(this.connectionParameters.getUrl()).append("'\n"); |
|
131 |
} else { |
|
132 |
builder.append("BasicDataSource pool status:\n"); |
|
133 |
builder.append(" Connection URL: '").append(this.dataSource.getUrl()).append("'\n"); |
|
134 |
if( this.dataSource.getInitialSize()>0 ) { |
|
135 |
builder.append(" InitialSize: ").append(this.dataSource.getInitialSize()).append(" (The initial number of connections that are created when the pool is started)\n"); |
|
136 |
} |
|
137 |
if( this.dataSource.isPoolPreparedStatements() ) { |
|
138 |
builder.append(" PoolPreparedStatements: ").append(this.dataSource.isPoolPreparedStatements()).append("\n"); |
|
139 |
builder.append(" MaxOpenPreparedStatements: ").append(this.dataSource.getMaxOpenPreparedStatements()).append(" (The maximum number of open statements that can be allocated from the statement pool at the same time, or non-positive for no limit)\n"); |
|
140 |
} |
|
141 |
builder.append(" MaxTotal: ").append(this.dataSource.getMaxTotal()).append(" (The maximum number of active connections that can be allocated from this pool at the same time)\n"); |
|
142 |
builder.append(" MaxIdle: ").append(this.dataSource.getMaxIdle()).append(" (The maximum number of connections that can remain idle in the pool)\n"); |
|
143 |
builder.append(" NumActive:").append(this.dataSource.getNumActive()).append(" (the current number of active connections)\n"); |
|
144 |
builder.append(" NumIdle:").append(this.dataSource.getNumIdle()).append(" (the current number of idle connections)\n"); |
|
106 | 145 |
} |
107 |
if( this.dataSource.isPoolPreparedStatements() ) { |
|
108 |
builder.append(" PoolPreparedStatements: ").append(this.dataSource.isPoolPreparedStatements()).append("\n"); |
|
109 |
builder.append(" MaxOpenPreparedStatements: ").append(this.dataSource.getMaxOpenPreparedStatements()).append(" (The maximum number of open statements that can be allocated from the statement pool at the same time, or non-positive for no limit)\n"); |
|
110 |
} |
|
111 |
builder.append(" MaxTotal: ").append(this.dataSource.getMaxTotal()).append(" (The maximum number of active connections that can be allocated from this pool at the same time)\n"); |
|
112 |
builder.append(" MaxIdle: ").append(this.dataSource.getMaxIdle()).append(" (The maximum number of connections that can remain idle in the pool)\n"); |
|
113 |
builder.append(" NumActive:").append(this.dataSource.getNumActive()).append(" (the current number of active connections)\n"); |
|
114 |
builder.append(" NumIdle:").append(this.dataSource.getNumIdle()).append(" (the current number of idle connections)\n"); |
|
115 | 146 |
return builder.toString(); |
116 | 147 |
} |
117 | 148 |
|
... | ... | |
148 | 179 |
return needRegisterDriver; |
149 | 180 |
} |
150 | 181 |
|
182 |
@Override |
|
151 | 183 |
public void registerDriver() throws SQLException { |
152 | 184 |
String className = this.connectionParameters.getJDBCDriverClassName(); |
153 | 185 |
if (className == null) { |
... | ... | |
179 | 211 |
|
180 | 212 |
@Override |
181 | 213 |
public String getStatus() { |
214 |
if( dataSource==null ) { |
|
215 |
return "Not polled"; |
|
216 |
} |
|
182 | 217 |
StringBuilder builder = new StringBuilder(); |
183 | 218 |
builder.append("Pool: "); |
184 | 219 |
builder.append(JDBCUtils.getHexId(dataSource)); |
Also available in: Unified diff