Moved to connection validation check, rather than using an own.

Implemented unimplemented connection methods.
This commit is contained in:
Robin Kupper
2011-05-03 10:47:44 +02:00
parent 96400cb1ca
commit 7d0ef7b264

View File

@@ -1,17 +1,3 @@
/**
* <b>Purpose:</b>Wrapper for JDBCConnectionDriver.<br>
* <b>Description:</b>http://java.sun.com/developer/onlineTraining/Programming/JDCBook/
* conpool.html<br>
* <b>Copyright:</b>Licensed under the Apache License, Version 2.0.
* http://www.apache.org/licenses/LICENSE-2.0<br>
* <b>Company:</b> SIMPL<br>
*
* @author schneimi
* @version $Id: JDCConnectionDriver.java 1224 2010-04-28 14:17:34Z
* michael.schneidt@arcor.de $<br>
* @link http://code.google.com/p/simpl09/
*/
package de.diddiz.util; package de.diddiz.util;
import java.sql.Array; import java.sql.Array;
@@ -23,7 +9,6 @@ import java.sql.DatabaseMetaData;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.NClob; import java.sql.NClob;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException; import java.sql.SQLClientInfoException;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLWarning; import java.sql.SQLWarning;
@@ -58,7 +43,7 @@ public class ConnectionPool {
for (int i = 0; i < connections.size(); i++) { for (int i = 0; i < connections.size(); i++) {
c = connections.elementAt(i); c = connections.elementAt(i);
if (c.lease()) { if (c.lease()) {
if (c.validate()) if (c.isValid())
return c; return c;
else { else {
c.terminate(); c.terminate();
@@ -68,7 +53,7 @@ public class ConnectionPool {
} }
c = new JDCConnection(DriverManager.getConnection(url, user, password)); c = new JDCConnection(DriverManager.getConnection(url, user, password));
c.lease(); c.lease();
if (!c.validate()) { if (!c.isValid()) {
c.terminate(); c.terminate();
throw new SQLException("Failed to validate a brand new connection"); throw new SQLException("Failed to validate a brand new connection");
} }
@@ -81,7 +66,7 @@ public class ConnectionPool {
final Enumeration<JDCConnection> connlist = connections.elements(); final Enumeration<JDCConnection> connlist = connections.elements();
while (connlist != null && connlist.hasMoreElements()) { while (connlist != null && connlist.hasMoreElements()) {
final JDCConnection conn = connlist.nextElement(); final JDCConnection conn = connlist.nextElement();
if (conn.inUse() && stale > conn.getLastUse() && !conn.validate()) if (conn.inUse() && stale > conn.getLastUse() && !conn.isValid())
removeConnection(conn); removeConnection(conn);
} }
} }
@@ -127,7 +112,7 @@ public class ConnectionPool {
public void terminate() { public void terminate() {
try { try {
conn.close(); conn.close();
} catch (SQLException ex) {} } catch (final SQLException ex) {}
} }
public synchronized boolean lease() { public synchronized boolean lease() {
@@ -140,27 +125,6 @@ public class ConnectionPool {
} }
} }
public boolean validate() {
Statement state = null;
ResultSet rs = null;
try {
conn.getMetaData();
state = conn.createStatement();
rs = state.executeQuery("/* ping */");
if (rs.next() && rs.getInt(1) == 1)
return true;
} catch (final SQLException ex) {
} finally {
try {
if (rs != null)
rs.close();
if (state != null)
state.close();
} catch (final SQLException ex) {}
}
return false;
}
public boolean inUse() { public boolean inUse() {
return inuse; return inuse;
} }
@@ -271,7 +235,7 @@ public class ConnectionPool {
@Override @Override
public Blob createBlob() throws SQLException { public Blob createBlob() throws SQLException {
return createBlob(); return conn.createBlob();
} }
@Override @Override
@@ -321,91 +285,105 @@ public class ConnectionPool {
@Override @Override
public Map<String, Class<?>> getTypeMap() throws SQLException { public Map<String, Class<?>> getTypeMap() throws SQLException {
return null; return conn.getTypeMap();
}
public boolean isValid() {
try {
return conn.isValid(1);
} catch (final SQLException ex) {
return false;
}
} }
@Override @Override
public boolean isValid(int timeout) throws SQLException { public boolean isValid(int timeout) throws SQLException {
return false; return conn.isValid(timeout);
} }
@Override @Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return null; return conn.prepareCall(sql, resultSetType, resultSetConcurrency);
} }
@Override @Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null; return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return null; return conn.prepareStatement(sql, autoGeneratedKeys);
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return null; return conn.prepareStatement(sql, columnIndexes);
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return null; return conn.prepareStatement(sql, columnNames);
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return null; return conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
} }
@Override @Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return null; return conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
} }
@Override @Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException { public void releaseSavepoint(Savepoint savepoint) throws SQLException {
conn.releaseSavepoint(savepoint);
} }
@Override @Override
public void rollback(Savepoint savepoint) throws SQLException { public void rollback(Savepoint savepoint) throws SQLException {
conn.rollback(savepoint);
} }
@Override @Override
public void setClientInfo(Properties properties) throws SQLClientInfoException { public void setClientInfo(Properties properties) throws SQLClientInfoException {
conn.setClientInfo(properties);
} }
@Override @Override
public void setClientInfo(String name, String value) throws SQLClientInfoException { public void setClientInfo(String name, String value) throws SQLClientInfoException {
conn.setClientInfo(name, value);
} }
@Override @Override
public void setHoldability(int holdability) throws SQLException { public void setHoldability(int holdability) throws SQLException {
conn.setHoldability(holdability);
} }
@Override @Override
public Savepoint setSavepoint() throws SQLException { public Savepoint setSavepoint() throws SQLException {
return null; return conn.setSavepoint();
} }
@Override @Override
public Savepoint setSavepoint(String name) throws SQLException { public Savepoint setSavepoint(String name) throws SQLException {
return null; return conn.setSavepoint(name);
} }
@Override @Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException { public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
conn.setTypeMap(map);
} }
@Override @Override
public boolean isWrapperFor(Class<?> iface) throws SQLException { public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false; return conn.isWrapperFor(iface);
} }
@Override @Override
public <T> T unwrap(Class<T> iface) throws SQLException { public <T> T unwrap(Class<T> iface) throws SQLException {
return null; return conn.unwrap(iface);
} }
} }
} }