[#1521] Expose Connection methods, such as commit(), rollback() and

similar transaction-related methods in Factory
This commit is contained in:
Lukas Eder 2012-06-30 10:06:17 +02:00
parent 1c92e88785
commit 8d277816c5
3 changed files with 293 additions and 3 deletions

View File

@ -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<Record> 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;
}

View File

@ -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 <code>Factory</code> 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 <code>Factory</code> without a {@link Connection}
*/
final static Factory getStaticFactory(SQLDialect dialect) {
final static FactoryOperations getStaticFactory(SQLDialect dialect) {
return DEFAULT_INSTANCES[dialect.ordinal()];
}

View File

@ -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");
}