From 7d0ef7b2646d199d7c93d579c5b2725792c97be6 Mon Sep 17 00:00:00 2001 From: Robin Kupper Date: Tue, 3 May 2011 10:47:44 +0200 Subject: [PATCH] Moved to connection validation check, rather than using an own. Implemented unimplemented connection methods. --- src/de/diddiz/util/ConnectionPool.java | 86 ++++++++++---------------- 1 file changed, 32 insertions(+), 54 deletions(-) diff --git a/src/de/diddiz/util/ConnectionPool.java b/src/de/diddiz/util/ConnectionPool.java index 3d2c09a..d576d10 100644 --- a/src/de/diddiz/util/ConnectionPool.java +++ b/src/de/diddiz/util/ConnectionPool.java @@ -1,17 +1,3 @@ -/** - * Purpose:Wrapper for JDBCConnectionDriver.
- * Description:http://java.sun.com/developer/onlineTraining/Programming/JDCBook/ - * conpool.html
- * Copyright:Licensed under the Apache License, Version 2.0. - * http://www.apache.org/licenses/LICENSE-2.0
- * Company: SIMPL
- * - * @author schneimi - * @version $Id: JDCConnectionDriver.java 1224 2010-04-28 14:17:34Z - * michael.schneidt@arcor.de $
- * @link http://code.google.com/p/simpl09/ - */ - package de.diddiz.util; import java.sql.Array; @@ -23,7 +9,6 @@ import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.NClob; import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLClientInfoException; import java.sql.SQLException; import java.sql.SQLWarning; @@ -58,7 +43,7 @@ public class ConnectionPool { for (int i = 0; i < connections.size(); i++) { c = connections.elementAt(i); if (c.lease()) { - if (c.validate()) + if (c.isValid()) return c; else { c.terminate(); @@ -68,7 +53,7 @@ public class ConnectionPool { } c = new JDCConnection(DriverManager.getConnection(url, user, password)); c.lease(); - if (!c.validate()) { + if (!c.isValid()) { c.terminate(); throw new SQLException("Failed to validate a brand new connection"); } @@ -81,7 +66,7 @@ public class ConnectionPool { final Enumeration connlist = connections.elements(); while (connlist != null && connlist.hasMoreElements()) { final JDCConnection conn = connlist.nextElement(); - if (conn.inUse() && stale > conn.getLastUse() && !conn.validate()) + if (conn.inUse() && stale > conn.getLastUse() && !conn.isValid()) removeConnection(conn); } } @@ -127,7 +112,7 @@ public class ConnectionPool { public void terminate() { try { conn.close(); - } catch (SQLException ex) {} + } catch (final SQLException ex) {} } 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() { return inuse; } @@ -271,7 +235,7 @@ public class ConnectionPool { @Override public Blob createBlob() throws SQLException { - return createBlob(); + return conn.createBlob(); } @Override @@ -321,91 +285,105 @@ public class ConnectionPool { @Override public Map> getTypeMap() throws SQLException { - return null; + return conn.getTypeMap(); + } + + public boolean isValid() { + try { + return conn.isValid(1); + } catch (final SQLException ex) { + return false; + } } @Override public boolean isValid(int timeout) throws SQLException { - return false; + return conn.isValid(timeout); } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { - return null; + return conn.prepareCall(sql, resultSetType, resultSetConcurrency); } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { - return null; + return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); } @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { - return null; + return conn.prepareStatement(sql, autoGeneratedKeys); } @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { - return null; + return conn.prepareStatement(sql, columnIndexes); } @Override public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { - return null; + return conn.prepareStatement(sql, columnNames); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { - return null; + return conn.prepareStatement(sql, resultSetType, resultSetConcurrency); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { - return null; + return conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); } @Override public void releaseSavepoint(Savepoint savepoint) throws SQLException { + conn.releaseSavepoint(savepoint); } @Override public void rollback(Savepoint savepoint) throws SQLException { + conn.rollback(savepoint); } @Override public void setClientInfo(Properties properties) throws SQLClientInfoException { + conn.setClientInfo(properties); } @Override public void setClientInfo(String name, String value) throws SQLClientInfoException { + conn.setClientInfo(name, value); } @Override public void setHoldability(int holdability) throws SQLException { + conn.setHoldability(holdability); } @Override public Savepoint setSavepoint() throws SQLException { - return null; + return conn.setSavepoint(); } @Override public Savepoint setSavepoint(String name) throws SQLException { - return null; + return conn.setSavepoint(name); } @Override public void setTypeMap(Map> map) throws SQLException { + conn.setTypeMap(map); } @Override public boolean isWrapperFor(Class iface) throws SQLException { - return false; + return conn.isWrapperFor(iface); } @Override public T unwrap(Class iface) throws SQLException { - return null; + return conn.unwrap(iface); } } }