From 2bc5b57575468e63b8986948447da4220eab9f66 Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Tue, 4 Mar 2014 22:35:09 +0900 Subject: [PATCH] Add catalog to the hikari configuration and into the connection state reset code --- .../java/com/zaxxer/hikari/HikariConfig.java | 11 +++ .../java/com/zaxxer/hikari/HikariPool.java | 4 +- .../zaxxer/hikari/proxy/ConnectionProxy.java | 76 +++++++++++++------ .../com/zaxxer/hikari/proxy/ProxyFactory.java | 2 +- 4 files changed, 69 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/HikariConfig.java b/src/main/java/com/zaxxer/hikari/HikariConfig.java index c7d30b61..c19e8779 100644 --- a/src/main/java/com/zaxxer/hikari/HikariConfig.java +++ b/src/main/java/com/zaxxer/hikari/HikariConfig.java @@ -60,6 +60,7 @@ public final class HikariConfig implements HikariConfigMBean private String connectionInitSql; private String connectionTestQuery; private String dataSourceClassName; + private String catalog; private String poolName; private boolean isAutoCommit; private boolean isInitializationFailFast; @@ -183,6 +184,16 @@ public final class HikariConfig implements HikariConfigMBean this.acquireRetryDelay = acquireRetryDelayMs; } + public String getCatalog() + { + return catalog; + } + + public void setCatalog(String catalog) + { + this.catalog = catalog; + } + public String getConnectionCustomizerClassName() { return connectionCustomizerClassName; diff --git a/src/main/java/com/zaxxer/hikari/HikariPool.java b/src/main/java/com/zaxxer/hikari/HikariPool.java index e5c6b74d..07ad99fa 100644 --- a/src/main/java/com/zaxxer/hikari/HikariPool.java +++ b/src/main/java/com/zaxxer/hikari/HikariPool.java @@ -61,6 +61,7 @@ public final class HikariPool implements HikariPoolMBean private final boolean isAutoCommit; private final boolean jdbc4ConnectionTest; private final boolean isRegisteredMbeans; + private final String catalog; private int transactionIsolation; private volatile boolean shutdown; private boolean debug; @@ -85,6 +86,7 @@ public final class HikariPool implements HikariPoolMBean this.isAutoCommit = configuration.isAutoCommit(); this.isRegisteredMbeans = configuration.isRegisterMbeans(); this.transactionIsolation = configuration.getTransactionIsolation(); + this.catalog = configuration.getCatalog(); this.debug = LOGGER.isDebugEnabled(); if (configuration.getDataSource() == null) @@ -392,7 +394,7 @@ public final class HikariPool implements HikariPoolMBean connectionCustomizer.customize(connection); } - IHikariConnectionProxy proxyConnection = (IHikariConnectionProxy) ProxyFactory.getProxyConnection(this, connection, transactionIsolation, isAutoCommit); + IHikariConnectionProxy proxyConnection = ProxyFactory.getProxyConnection(this, connection, transactionIsolation, isAutoCommit, catalog); String initSql = configuration.getConnectionInitSql(); if (initSql != null && initSql.length() > 0) diff --git a/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java b/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java index d1c814bc..8fcc3c69 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java @@ -46,18 +46,19 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy protected final Connection delegate; + private final long creationTime; private final FastStatementList openStatements; private final HikariPool parentPool; private final int defaultIsolationLevel; private final boolean defaultAutoCommit; + private final String defaultCatalog; private final AtomicInteger state; private boolean isClosed; private boolean forceClose; private boolean isTransactionIsolationDirty; private boolean isAutoCommitDirty; - - private final long creationTime; + private boolean isCatalogDirty; private volatile long lastAccess; private StackTraceElement[] leakTrace; @@ -65,6 +66,7 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy private final int hashCode; + // static initializer static { @@ -77,12 +79,13 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy SQL_ERRORS.add("JZ0C1"); // Sybase disconnect error } - protected ConnectionProxy(HikariPool pool, Connection connection, int defaultIsolationLevel, boolean defaultAutoCommit) + protected ConnectionProxy(HikariPool pool, Connection connection, int defaultIsolationLevel, boolean defaultAutoCommit, String defaultCatalog) { this.parentPool = pool; this.delegate = connection; this.defaultIsolationLevel = defaultIsolationLevel; this.defaultAutoCommit = defaultAutoCommit; + this.defaultCatalog = defaultCatalog; this.state = new AtomicInteger(); this.creationTime = lastAccess = System.currentTimeMillis(); @@ -173,13 +176,41 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy } } - private final T trackStatement(T statement) + private T trackStatement(T statement) { openStatements.add(statement); return statement; } + private void resetConnectionState() throws SQLException + { + if (!delegate.getAutoCommit()) + { + delegate.rollback(); + } + + if (isAutoCommitDirty) + { + delegate.setAutoCommit(defaultAutoCommit); + isAutoCommitDirty = false; + } + + if (isTransactionIsolationDirty) + { + delegate.setTransactionIsolation(defaultIsolationLevel); + isTransactionIsolationDirty = false; + } + + if (isCatalogDirty && defaultCatalog != null) + { + delegate.setCatalog(defaultCatalog); + isCatalogDirty = false; + } + + delegate.clearWarnings(); + } + // ********************************************************************** // IBagManagable Methods // ********************************************************************** @@ -234,24 +265,7 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy openStatements.clear(); } - if (!delegate.getAutoCommit()) - { - delegate.rollback(); - } - - if (isAutoCommitDirty) - { - delegate.setAutoCommit(defaultAutoCommit); - isAutoCommitDirty = false; - } - - if (isTransactionIsolationDirty) - { - delegate.setTransactionIsolation(defaultIsolationLevel); - isTransactionIsolationDirty = false; - } - - delegate.clearWarnings(); + resetConnectionState(); } catch (SQLException e) { @@ -486,6 +500,7 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy /** {@inheritDoc} */ public final void setAutoCommit(boolean autoCommit) throws SQLException { + checkClosed(); try { delegate.setAutoCommit(autoCommit); @@ -501,6 +516,7 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy /** {@inheritDoc} */ public final void setTransactionIsolation(int level) throws SQLException { + checkClosed(); try { delegate.setTransactionIsolation(level); @@ -513,6 +529,22 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy } } + @Override + public final void setCatalog(String catalog) throws SQLException + { + checkClosed(); + try + { + delegate.setCatalog(catalog); + isCatalogDirty = !catalog.equals(defaultCatalog); + } + catch (SQLException e) + { + checkException(e); + throw e; + } + } + /** {@inheritDoc} */ public final boolean isWrapperFor(Class iface) throws SQLException { diff --git a/src/main/java/com/zaxxer/hikari/proxy/ProxyFactory.java b/src/main/java/com/zaxxer/hikari/proxy/ProxyFactory.java index b40f9921..06348822 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/ProxyFactory.java +++ b/src/main/java/com/zaxxer/hikari/proxy/ProxyFactory.java @@ -30,7 +30,7 @@ import com.zaxxer.hikari.HikariPool; */ public final class ProxyFactory { - public static Connection getProxyConnection(HikariPool pool, Connection connection, int defaultIsolationLevel, boolean defaultAutoCommit) + public static IHikariConnectionProxy getProxyConnection(HikariPool pool, Connection connection, int defaultIsolationLevel, boolean defaultAutoCommit, String defaultCatalog) { // Body is injected by JavassistProxyFactory return null;