/** * Copyright 2010 JBoss Inc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package bitronix.tm.resource.jdbc.lrc; import java.sql.*; import java.util.Map; /** * Connection handle implementation for a non-XA JDBC resource emulating XA with Last Resource Commit. * <p>© <a href="http://www.bitronix.be">Bitronix Software</a></p> * * @author lorban */ public class LrcConnectionHandle implements Connection { private Connection delegate; private LrcXAResource xaResource; public LrcConnectionHandle(LrcXAResource xaResource, Connection delegate) { this.delegate = delegate; this.xaResource = xaResource; } public Connection getConnection() { return delegate; } private Connection getDelegate() throws SQLException { if (delegate == null) throw new SQLException("connection is closed"); return delegate; } /* wrapped Connection methods that have special XA semantics */ public void close() throws SQLException { delegate = null; } public boolean isClosed() throws SQLException { return delegate == null; } public boolean getAutoCommit() throws SQLException { return getDelegate().getAutoCommit(); } public void setAutoCommit(boolean autoCommit) throws SQLException { if (xaResource.getState() != LrcXAResource.NO_TX && autoCommit) throw new SQLException("XA transaction started, cannot enable autocommit mode"); getDelegate().setAutoCommit(autoCommit); } public void commit() throws SQLException { if (xaResource.getState() != LrcXAResource.NO_TX) throw new SQLException("XA transaction started, cannot call commit directly on connection"); getDelegate().commit(); } public void rollback() throws SQLException { if (xaResource.getState() != LrcXAResource.NO_TX) throw new SQLException("XA transaction started, cannot call rollback directly on connection"); getDelegate().rollback(); } public void rollback(Savepoint savepoint) throws SQLException { if (xaResource.getState() != LrcXAResource.NO_TX) throw new SQLException("XA transaction started, cannot call rollback directly on connection"); getDelegate().rollback(savepoint); } public String toString() { return "a JDBC LrcConnectionHandle on " + xaResource; } /* dumb wrapping of Connection methods */ public Statement createStatement() throws SQLException { return getDelegate().createStatement(); } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return getDelegate().createStatement(resultSetType, resultSetConcurrency); } public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return getDelegate().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); } public CallableStatement prepareCall(String sql) throws SQLException { return getDelegate().prepareCall(sql); } public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return getDelegate().prepareCall(sql, resultSetType, resultSetConcurrency); } public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return getDelegate().prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); } public PreparedStatement prepareStatement(String sql) throws SQLException { return getDelegate().prepareStatement(sql); } public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return getDelegate().prepareStatement(sql, autoGeneratedKeys); } public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return getDelegate().prepareStatement(sql, resultSetType, resultSetConcurrency); } public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return getDelegate().prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); } public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { return getDelegate().prepareStatement(sql, columnIndexes); } public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return getDelegate().prepareStatement(sql, columnNames); } public int getHoldability() throws SQLException { return getDelegate().getHoldability(); } public int getTransactionIsolation() throws SQLException { return getDelegate().getTransactionIsolation(); } public void clearWarnings() throws SQLException { getDelegate().clearWarnings(); } public boolean isReadOnly() throws SQLException { return getDelegate().isReadOnly(); } public void setHoldability(int holdability) throws SQLException { getDelegate().setHoldability(holdability); } public void setTransactionIsolation(int level) throws SQLException { getDelegate().setTransactionIsolation(level); } public void setReadOnly(boolean readOnly) throws SQLException { getDelegate().setReadOnly(readOnly); } public String getCatalog() throws SQLException { return getDelegate().getCatalog(); } public void setCatalog(String catalog) throws SQLException { getDelegate().setCatalog(catalog); } public DatabaseMetaData getMetaData() throws SQLException { return getDelegate().getMetaData(); } public SQLWarning getWarnings() throws SQLException { return getDelegate().getWarnings(); } public Savepoint setSavepoint() throws SQLException { return getDelegate().setSavepoint(); } public void releaseSavepoint(Savepoint savepoint) throws SQLException { getDelegate().releaseSavepoint(savepoint); } public Map getTypeMap() throws SQLException { return getDelegate().getTypeMap(); } public void setTypeMap(Map map) throws SQLException { getDelegate().setTypeMap(map); } public String nativeSQL(String sql) throws SQLException { return getDelegate().nativeSQL(sql); } public Savepoint setSavepoint(String name) throws SQLException { return getDelegate().setSavepoint(name); } }