diff --git a/jOOQ/src/main/java/org/jooq/tools/jdbc/BatchedConnection.java b/jOOQ/src/main/java/org/jooq/tools/jdbc/BatchedConnection.java index 0102f948ec..21655ce231 100644 --- a/jOOQ/src/main/java/org/jooq/tools/jdbc/BatchedConnection.java +++ b/jOOQ/src/main/java/org/jooq/tools/jdbc/BatchedConnection.java @@ -79,8 +79,8 @@ import java.util.regex.Pattern; */ public class BatchedConnection extends DefaultConnection { - private String lastSQL; - private PreparedStatement lastStatement; + String lastSQL; + PreparedStatement lastStatement; public BatchedConnection(Connection delegate) { super(delegate); @@ -90,17 +90,20 @@ public class BatchedConnection extends DefaultConnection { // XXX: Utilities // ------------------------------------------------------------------------- - private void checkBatch(String sql) throws SQLException { + private void executeLastBatch(String sql) throws SQLException { if (!sql.equals(lastSQL)) executeLastBatch(); } private void executeLastBatch() throws SQLException { if (lastStatement != null) { - lastStatement.executeBatch(); + if (lastStatement instanceof BatchedPreparedStatement && ((BatchedPreparedStatement) lastStatement).batches > 0) + lastStatement.executeBatch(); + safeClose(lastStatement); - clearLastBatch(); } + + clearLastBatch(); } void clearLastBatch() { @@ -184,16 +187,16 @@ public class BatchedConnection extends DefaultConnection { @Override public PreparedStatement prepareStatement(String sql) throws SQLException { - checkBatch(sql); + executeLastBatch(sql); return lastStatement != null ? lastStatement : (lastStatement = prepareStatement0(lastSQL = sql)); } + // TODO: Can we implement this in a more sophisticated way without invoking the costly parser? static final Pattern P_DML = Pattern.compile("\\s*(?i:delete|insert|merge|update).*"); private PreparedStatement prepareStatement0(String sql) throws SQLException { - - // TODO: More sophistication here PreparedStatement result = super.prepareStatement(sql); + return P_DML.matcher(sql).matches() ? new BatchedPreparedStatement(this, result) : result; diff --git a/jOOQ/src/main/java/org/jooq/tools/jdbc/BatchedPreparedStatement.java b/jOOQ/src/main/java/org/jooq/tools/jdbc/BatchedPreparedStatement.java index 0565b3cb83..9c3d06805b 100644 --- a/jOOQ/src/main/java/org/jooq/tools/jdbc/BatchedPreparedStatement.java +++ b/jOOQ/src/main/java/org/jooq/tools/jdbc/BatchedPreparedStatement.java @@ -41,6 +41,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import org.jooq.tools.JooqLogger; + /** * A batched statement. *
@@ -53,15 +55,32 @@ import java.sql.SQLException; */ public class BatchedPreparedStatement extends DefaultPreparedStatement { - private int batches; + private static final JooqLogger log = JooqLogger.getLogger(BatchedPreparedStatement.class); + int batches; public BatchedPreparedStatement(BatchedConnection connection, PreparedStatement delegate) { super(delegate, connection); } - // ------------------------------------------------------------------------- - // XXX: Utilities - // ------------------------------------------------------------------------- + public BatchedConnection getBatchedConnection() throws SQLException { + return (BatchedConnection) super.getConnection(); + } + + private void resetBatches() { + batches = 0; + } + + private void logBatch() throws SQLException { + if (log.isDebugEnabled()) + log.debug("BatchedStatement", "Batched " + batches + " times: " + getBatchedConnection().lastSQL); + } + + private void logExecution() throws SQLException { + if (log.isDebugEnabled()) + log.debug("BatchedStatement", "Executed " + batches + " batches: " + getBatchedConnection().lastSQL); + + resetBatches(); + } // ------------------------------------------------------------------------- // XXX: Executing queries @@ -108,21 +127,18 @@ public class BatchedPreparedStatement extends DefaultPreparedStatement { @Override public void addBatch() throws SQLException { batches++; + logBatch(); super.addBatch(); } - @Override - public void addBatch(String sql) throws SQLException { - super.addBatch(sql); - } - @Override public void clearBatch() throws SQLException { - super.clearBatch(); + throw new UnsupportedOperationException("Clearing a batch is not yet supported"); } @Override public int[] executeBatch() throws SQLException { + logExecution(); return super.executeBatch(); } @@ -130,6 +146,7 @@ public class BatchedPreparedStatement extends DefaultPreparedStatement { @Override public long[] executeLargeBatch() throws SQLException { + logExecution(); return super.executeLargeBatch(); } @@ -139,6 +156,11 @@ public class BatchedPreparedStatement extends DefaultPreparedStatement { // XXX: Unsupported static statement execution features // ------------------------------------------------------------------------- + @Override + public void addBatch(String sql) throws SQLException { + throw new UnsupportedOperationException("No static statement methods can be called"); + } + @Override public boolean execute(String sql) throws SQLException { throw new UnsupportedOperationException("No static statement methods can be called"); @@ -214,10 +236,10 @@ public class BatchedPreparedStatement extends DefaultPreparedStatement { @Override public ResultSet executeQuery() throws SQLException { - if (batches > 0) - throw new UnsupportedOperationException("Cannot batch result queries"); - else + if (batches == 0) return super.executeQuery(); + else + throw new UnsupportedOperationException("Cannot batch result queries"); } @Override