diff -ruN src.orig/net/lemnik/eodsql/impl/BaseQueryImpl.java src/net/lemnik/eodsql/impl/BaseQueryImpl.java --- src.orig/net/lemnik/eodsql/impl/BaseQueryImpl.java 2009-07-28 23:59:47.000000000 +0200 +++ src/net/lemnik/eodsql/impl/BaseQueryImpl.java 2009-07-28 23:50:57.000000000 +0200 @@ -21,6 +21,7 @@ import javax.sql.DataSource; import net.lemnik.eodsql.BaseQuery; +import net.lemnik.eodsql.EoDException; import net.lemnik.eodsql.QueryTool; import net.lemnik.eodsql.InvalidQueryException; @@ -38,7 +39,7 @@ protected Map methods = new HashMap(); - private ConnectionSource connectionSource; + protected ConnectionSource connectionSource; private ExceptionTranslator exceptionTranslator; @@ -170,6 +171,11 @@ } } + protected Context createContext(Annotation annotation, final Object[] args) + { + return new Context(annotation, args); + } + static interface Callable { public Object invoke(Method method, Object[] args) throws Throwable; @@ -212,8 +218,7 @@ final Object[] args) throws Throwable { - final Context context = - new Context(annotation, args); + final Context context = createContext(annotation, args); final Resource connection = new ConnectionSourceConnectionResource(connectionSource); @@ -229,7 +234,7 @@ } } } - + AbstractMethodImplementation getMethodImpl() { return (AbstractMethodImplementation) implementation; @@ -284,16 +289,19 @@ static class DataSourceConnectionSource implements ConnectionSource { - private static final ThreadLocal CONNECTION_UTILS = + private final ThreadLocal CONNECTION_UTILS = new ThreadLocal(); private final DataSource datasource; + private final boolean autoCommit; + private final Set connections = Collections.synchronizedSet( new HashSet()); - DataSourceConnectionSource(final DataSource datasource) { + DataSourceConnectionSource(final DataSource datasource, final boolean autoCommit) { this.datasource = datasource; + this.autoCommit = autoCommit; } @Override @@ -319,6 +327,12 @@ if(util == null || util.connection.isClosed()) { final Connection tmp = datasource.getConnection(); + try { + tmp.setAutoCommit(autoCommit); + } catch(SQLException ex) { + throw new EoDException("Cannot set auto-commit on Connection", ex); + } + connections.add(tmp); util = new ConnectionUtil(tmp); diff -ruN src.orig/net/lemnik/eodsql/impl/DefaultQueryFactory.java src/net/lemnik/eodsql/impl/DefaultQueryFactory.java --- src.orig/net/lemnik/eodsql/impl/DefaultQueryFactory.java 2009-07-28 23:58:39.000000000 +0200 +++ src/net/lemnik/eodsql/impl/DefaultQueryFactory.java 2009-07-27 14:10:26.000000000 +0200 @@ -103,15 +103,9 @@ final Class query, final ClassLoader loader) throws InvalidQueryException { if(TransactionQuery.class.isAssignableFrom(query)) { - try { - return constructProxy(loader, query, new TransactionQueryImpl( - dataSource.getConnection(), query)); - } catch(SQLException ex) { - throw new InvalidQueryException("Couldn't get a Connection from the DataSource " + - "for a TransactionQuery", ex); - } + return constructProxy(loader, query, new TransactionQueryImpl(dataSource, query)); } else { - return construct(new BaseQueryImpl.DataSourceConnectionSource(dataSource), query, loader); + return construct(new BaseQueryImpl.DataSourceConnectionSource(dataSource, true), query, loader); } } diff -ruN src.orig/net/lemnik/eodsql/impl/TransactionQueryImpl.java src/net/lemnik/eodsql/impl/TransactionQueryImpl.java --- src.orig/net/lemnik/eodsql/impl/TransactionQueryImpl.java 2009-07-28 23:58:39.000000000 +0200 +++ src/net/lemnik/eodsql/impl/TransactionQueryImpl.java 2009-07-27 14:38:57.000000000 +0200 @@ -1,59 +1,82 @@ package net.lemnik.eodsql.impl; +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.sql.Savepoint; import java.sql.Connection; import java.sql.SQLException; +import javax.sql.DataSource; + import net.lemnik.eodsql.BaseQuery; -import net.lemnik.eodsql.InvalidQueryException; +import net.lemnik.eodsql.EoDException; import net.lemnik.eodsql.TransactionQuery; +import net.lemnik.eodsql.spi.Context; /** * Created on 2008/07/23 * @author Jason Morris */ class TransactionQueryImpl extends BaseQueryImpl { - protected final Connection connection; + final boolean noAutoClose; public TransactionQueryImpl(final Connection connection, final Class clazz) { super(new SingleConnectionSource(connection), clazz, TransactionQuery.class); - this.connection = connection; + this.noAutoClose = false; addTransactionMethods(); try { connection.setAutoCommit(false); } catch(SQLException ex) { - throw new InvalidQueryException("Cannot set auto-commit on Connection", ex); + throw new EoDException("Cannot set auto-commit on Connection", ex); } } + public TransactionQueryImpl(final DataSource datasource, + final Class clazz) { + + super(new DataSourceConnectionSource(datasource, false), clazz, TransactionQuery.class); + this.noAutoClose = true; + addTransactionMethods(); + } + + @Override + protected Context createContext(Annotation annotation, final Object[] args) + { + final Context context = new Context(annotation, args); + if (noAutoClose) + { + context.setAutoclose(false); + } + return context; + } + @Override protected void close() throws SQLException { super.close(); } protected void commit() throws SQLException { - connection.commit(); + connectionSource.getConnection().commit(); } protected void rollback() throws SQLException { - connection.rollback(); + connectionSource.getConnection().rollback(); } protected void rollback(Savepoint savepoint) throws SQLException { - connection.rollback(savepoint); + connectionSource.getConnection().rollback(savepoint); } protected Savepoint setSavepoint() throws SQLException { - return connection.setSavepoint(); + return connectionSource.getConnection().setSavepoint(); } protected Savepoint setSavepoint(String name) throws SQLException { - return connection.setSavepoint(name); + return connectionSource.getConnection().setSavepoint(name); } protected void addTransactionMethods() { diff -ruN src.orig/net/lemnik/eodsql/spi/Context.java src/net/lemnik/eodsql/spi/Context.java --- src.orig/net/lemnik/eodsql/spi/Context.java 2009-07-28 23:58:39.000000000 +0200 +++ src/net/lemnik/eodsql/spi/Context.java 2009-07-27 14:45:48.000000000 +0200 @@ -80,7 +80,10 @@ @Override protected void finalize() throws Exception { - close(); + if (autoclose) + { + close(); + } } /**