This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 2ac65b30884 fix 18904(#18904) (#18905)
2ac65b30884 is described below

commit 2ac65b3088460ac939704d4b551df6b5d2c0198b
Author: yunmengmeng <[email protected]>
AuthorDate: Sat Jul 30 09:29:25 2022 +0800

    fix 18904(#18904) (#18905)
    
    * fix 18904(#18904)
    
    Co-authored-by: ‘huxin’ <‘[email protected]’>
---
 .../statement/ShardingSpherePreparedStatement.java |  1 +
 .../ReadwriteSplittingPreparedStatementTest.java   | 26 ++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index 639a9a3b832..f8b7a99d9d0 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -524,6 +524,7 @@ public final class ShardingSpherePreparedStatement extends 
AbstractPreparedState
     private void clearPrevious() {
         statements.clear();
         parameterSets.clear();
+        generatedValues.clear();
     }
     
     private Optional<GeneratedKeyContext> findGeneratedKey(final 
ExecutionContext executionContext) {
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ReadwriteSplittingPreparedStatementTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ReadwriteSplittingPreparedStatementTest.java
index c8d57562102..f94fafc88dd 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ReadwriteSplittingPreparedStatementTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ReadwriteSplittingPreparedStatementTest.java
@@ -20,12 +20,14 @@ package 
org.apache.shardingsphere.driver.jdbc.core.statement;
 import 
org.apache.shardingsphere.driver.jdbc.base.AbstractShardingSphereDataSourceForReadwriteSplittingTest;
 import org.junit.Test;
 
+import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 
 import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
@@ -92,4 +94,28 @@ public final class ReadwriteSplittingPreparedStatementTest 
extends AbstractShard
             assertFalse(generatedKeys.next());
         }
     }
+    
+    @Test
+    public void assertGetGeneratedKeysWithPrimaryKeyIsNullInTransactional() 
throws SQLException {
+        try (
+                Connection connection = getReadwriteSplittingDataSource()
+                        .getConnection();
+                PreparedStatement preparedStatement = 
connection.prepareStatement("INSERT INTO t_config(id, status) VALUES(?, ?);", 
Statement.RETURN_GENERATED_KEYS)) {
+            connection.setAutoCommit(false);
+            Object lastGeneratedId = null;
+            Object generatedId = null;
+            for (int i = 1; i <= 3; i++) {
+                preparedStatement.setObject(1, null);
+                preparedStatement.setString(2, "OK");
+                preparedStatement.executeUpdate();
+                ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
+                assertTrue(generatedKeys.next());
+                generatedId = generatedKeys.getObject(1);
+                assertThat(generatedId, not(lastGeneratedId));
+                lastGeneratedId = generatedId;
+                assertFalse(generatedKeys.next());
+            }
+            connection.commit();
+        }
+    }
 }

Reply via email to