From 8d277816c50671edcaf7d7178d265d99eb68a01c Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 30 Jun 2012 10:06:17 +0200 Subject: [PATCH] [#1521] Expose Connection methods, such as commit(), rollback() and similar transaction-related methods in Factory --- .../main/java/org/jooq/FactoryOperations.java | 65 +++++++ jOOQ/src/main/java/org/jooq/impl/Factory.java | 168 +++++++++++++++++- .../main/java/org/jooq/impl/FactoryProxy.java | 63 ++++++- 3 files changed, 293 insertions(+), 3 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/FactoryOperations.java b/jOOQ/src/main/java/org/jooq/FactoryOperations.java index 7e2e78d7ed..0f47d45eea 100644 --- a/jOOQ/src/main/java/org/jooq/FactoryOperations.java +++ b/jOOQ/src/main/java/org/jooq/FactoryOperations.java @@ -53,6 +53,7 @@ import java.math.BigInteger; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.Savepoint; import java.sql.Statement; import java.util.Collection; import java.util.List; @@ -1608,4 +1609,68 @@ public interface FactoryOperations extends Configuration { */ @Support ResultQuery resultQuery(String sql, QueryPart... parts); + + // ------------------------------------------------------------------------- + // XXX Convenience methods accessing the underlying Connection + // ------------------------------------------------------------------------- + + /** + * Convenience method to access {@link Connection#getTransactionIsolation()} + */ + int getTransactionIsolation() throws DataAccessException; + + /** + * Convenience method to access {@link Connection#setTransactionIsolation(int)} + */ + void setTransactionIsolation(int level) throws DataAccessException; + + /** + * Convenience method to access {@link Connection#getHoldability()} + */ + int getHoldability() throws DataAccessException; + + /** + * Convenience method to access {@link Connection#setHoldability(int)} + */ + void setHoldability(int holdability) throws DataAccessException; + + /** + * Convenience method to access {@link Connection#getAutoCommit()} + */ + boolean getAutoCommit() throws DataAccessException; + + /** + * Convenience method to access {@link Connection#setAutoCommit(boolean)} + */ + void setAutoCommit(boolean autoCommit) throws DataAccessException; + + /** + * Convenience method to access {@link Connection#releaseSavepoint(Savepoint)} + */ + void releaseSavepoint(Savepoint savepoint) throws DataAccessException; + + /** + * Convenience method to access {@link Connection#setSavepoint(String)} + */ + Savepoint setSavepoint(String name) throws DataAccessException; + + /** + * Convenience method to access {@link Connection#setSavepoint()} + */ + Savepoint setSavepoint() throws DataAccessException; + + /** + * Convenience method to access {@link Connection#rollback(Savepoint)} + */ + void rollback(Savepoint savepoint) throws DataAccessException; + + /** + * Convenience method to access {@link Connection#rollback()} + */ + void rollback() throws DataAccessException; + + /** + * Convenience method to access {@link Connection#commit()} + */ + void commit() throws DataAccessException; } diff --git a/jOOQ/src/main/java/org/jooq/impl/Factory.java b/jOOQ/src/main/java/org/jooq/impl/Factory.java index e93c80ba37..93dde1e856 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Factory.java +++ b/jOOQ/src/main/java/org/jooq/impl/Factory.java @@ -65,6 +65,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; +import java.sql.Savepoint; import java.sql.Time; import java.sql.Timestamp; import java.util.Arrays; @@ -358,6 +359,9 @@ public class Factory implements FactoryOperations { } } + /** + * {@inheritDoc} + */ @Override public final void setConnection(Connection connection) { this.connection = connection; @@ -404,6 +408,166 @@ public class Factory implements FactoryOperations { return data.put(key, value); } + // ------------------------------------------------------------------------- + // XXX Convenience methods accessing the underlying Connection + // ------------------------------------------------------------------------- + + /** + * {@inheritDoc} + */ + @Override + public final void commit() throws DataAccessException { + try { + getConnection().commit(); + } + catch (Exception e) { + throw new DataAccessException("Cannot commit transaction", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final void rollback() throws DataAccessException { + try { + getConnection().rollback(); + } + catch (Exception e) { + throw new DataAccessException("Cannot rollback transaction", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final void rollback(Savepoint savepoint) throws DataAccessException { + try { + getConnection().rollback(savepoint); + } + catch (Exception e) { + throw new DataAccessException("Cannot rollback transaction", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final Savepoint setSavepoint() throws DataAccessException { + try { + return getConnection().setSavepoint(); + } + catch (Exception e) { + throw new DataAccessException("Cannot set savepoint", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final Savepoint setSavepoint(String name) throws DataAccessException { + try { + return getConnection().setSavepoint(name); + } + catch (Exception e) { + throw new DataAccessException("Cannot set savepoint", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final void releaseSavepoint(Savepoint savepoint) throws DataAccessException { + try { + getConnection().releaseSavepoint(savepoint); + } + catch (Exception e) { + throw new DataAccessException("Cannot release savepoint", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final void setAutoCommit(boolean autoCommit) throws DataAccessException { + try { + getConnection().setAutoCommit(autoCommit); + } + catch (Exception e) { + throw new DataAccessException("Cannot set autoCommit", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final boolean getAutoCommit() throws DataAccessException { + try { + return getConnection().getAutoCommit(); + } + catch (Exception e) { + throw new DataAccessException("Cannot get autoCommit", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final void setHoldability(int holdability) throws DataAccessException { + try { + getConnection().setHoldability(holdability); + } + catch (Exception e) { + throw new DataAccessException("Cannot set holdability", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final int getHoldability() throws DataAccessException { + try { + return getConnection().getHoldability(); + } + catch (Exception e) { + throw new DataAccessException("Cannot get holdability", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final void setTransactionIsolation(int level) throws DataAccessException { + try { + getConnection().setTransactionIsolation(level); + } + catch (Exception e) { + throw new DataAccessException("Cannot set transactionIsolation", e); + } + } + + /** + * {@inheritDoc} + */ + @Override + public final int getTransactionIsolation() throws DataAccessException { + try { + return getConnection().getTransactionIsolation(); + } + catch (Exception e) { + throw new DataAccessException("Cannot get transactionIsolation", e); + } + } + // ------------------------------------------------------------------------- // XXX RenderContext and BindContext accessors // ------------------------------------------------------------------------- @@ -5910,14 +6074,14 @@ public class Factory implements FactoryOperations { /** * Get a default Factory without a {@link Connection} */ - final static Factory getNewFactory(SQLDialect dialect) { + final static FactoryOperations getNewFactory(SQLDialect dialect) { return getNewFactory(DEFAULT_INSTANCES[dialect.ordinal()]); } /** * Get a default Factory without a {@link Connection} */ - final static Factory getStaticFactory(SQLDialect dialect) { + final static FactoryOperations getStaticFactory(SQLDialect dialect) { return DEFAULT_INSTANCES[dialect.ordinal()]; } diff --git a/jOOQ/src/main/java/org/jooq/impl/FactoryProxy.java b/jOOQ/src/main/java/org/jooq/impl/FactoryProxy.java index 7981e00409..1428d6c657 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FactoryProxy.java +++ b/jOOQ/src/main/java/org/jooq/impl/FactoryProxy.java @@ -39,6 +39,7 @@ import java.lang.reflect.Constructor; import java.math.BigInteger; import java.sql.Connection; import java.sql.ResultSet; +import java.sql.Savepoint; import java.util.Collection; import java.util.List; import java.util.Map; @@ -580,7 +581,67 @@ public final class FactoryProxy implements FactoryOperations { return getDelegate().executeDeleteOne(table, condition); } - private Factory getDelegate() { + @Override + public final int getTransactionIsolation() throws DataAccessException { + return getDelegate().getTransactionIsolation(); + } + + @Override + public final void setTransactionIsolation(int level) throws DataAccessException { + getDelegate().setTransactionIsolation(level); + } + + @Override + public final int getHoldability() throws DataAccessException { + return getDelegate().getHoldability(); + } + + @Override + public final void setHoldability(int holdability) throws DataAccessException { + getDelegate().setHoldability(holdability); + } + + @Override + public final boolean getAutoCommit() throws DataAccessException { + return getDelegate().getAutoCommit(); + } + + @Override + public final void setAutoCommit(boolean autoCommit) throws DataAccessException { + getDelegate().setAutoCommit(autoCommit); + } + + @Override + public final void releaseSavepoint(Savepoint savepoint) throws DataAccessException { + getDelegate().releaseSavepoint(savepoint); + } + + @Override + public final Savepoint setSavepoint(String name) throws DataAccessException { + return getDelegate().setSavepoint(name); + } + + @Override + public final Savepoint setSavepoint() throws DataAccessException { + return getDelegate().setSavepoint(); + } + + @Override + public final void rollback(Savepoint savepoint) throws DataAccessException { + getDelegate().rollback(savepoint); + } + + @Override + public final void rollback() throws DataAccessException { + getDelegate().rollback(); + } + + @Override + public final void commit() throws DataAccessException { + getDelegate().commit(); + } + + private FactoryOperations getDelegate() { if (dataSource == null || dialect == null) { throw new DataAccessException("Both dataSource and dialect properties should be set"); }