Skip to content

Commit

Permalink
#789 Frontport #788 to Jaybird 6
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Mar 13, 2024
1 parent ab926a0 commit c088871
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/org/firebirdsql/jdbc/FBPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,7 @@ private Batch requireBatch() throws SQLException {

// NOTE: This is not used for FBCallableStatement!
private Batch createBatch() throws SQLException {
if (connection != null && connection.isUseServerBatch()
&& fbStatement != null && fbStatement.supportBatchUpdates()
&& !isGeneratedKeyQuery()) {
if (canExecuteUsingServerBatch()) {
return new ServerBatch(
FbBatchConfig.of(FbBatchConfig.HALT_AT_FIRST_ERROR, FbBatchConfig.UPDATE_COUNTS,
FbBatchConfig.SERVER_DEFAULT_DETAILED_ERRORS, connection.getServerBatchBufferSize()),
Expand All @@ -618,6 +616,17 @@ private Batch createBatch() throws SQLException {
}
}

private boolean canExecuteUsingServerBatch() {
// enabled for connection
return connection != null && connection.isUseServerBatch()
// supported by statement implementation
&& fbStatement != null && fbStatement.supportBatchUpdates()
// server batch execution throws isc_batch_param when executing a statement without parameters
&& fbStatement.getParameterDescriptor().getCount() != 0
// server batch execution cannot produce rows from RETURNING
&& !isGeneratedKeyQuery();
}

@Override
public void addBatch() throws SQLException {
checkValidity();
Expand Down
22 changes: 22 additions & 0 deletions src/test/org/firebirdsql/jdbc/FBPreparedStatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,28 @@ create procedure char_return returns (val char(5)) as
}
}

/**
* Test for <a href="https://github.com/FirebirdSQL/jaybird/issues/788">jaybird#788</a>
*/
@ParameterizedTest(name = "[{index}] useServerBatch = {0}")
@ValueSource(booleans = { true, false })
void executeBatchWithoutParameters(boolean useServerBatch) throws Exception {
executeCreateTable(con, CREATE_TABLE);

Properties props = FBTestProperties.getDefaultPropertiesForConnection();
props.setProperty(PropertyNames.useServerBatch, String.valueOf(useServerBatch));
try (var con = DriverManager.getConnection(FBTestProperties.getUrl(), props);
var pstmt = con.prepareStatement("insert into test default values")) {
pstmt.addBatch();
assertDoesNotThrow(pstmt::executeBatch);
}
try (var stmt = con.createStatement();
var rs = stmt.executeQuery("select count(*) from test")) {
assertTrue(rs.next(), "expected a row");
assertEquals(1, rs.getInt(1), "Expected one row in table test");
}
}

private void prepareTestData() throws SQLException {
con.setAutoCommit(false);
try (PreparedStatement pstmt = con.prepareStatement(INSERT_DATA)) {
Expand Down

0 comments on commit c088871

Please sign in to comment.