IGNITE-6465: JDBC thin driver: added SQLSTATE propagation for 
BatchUpdateException. This closes #2719.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/f8ae8f9b
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/f8ae8f9b
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/f8ae8f9b

Branch: refs/heads/ignite-6181-2
Commit: f8ae8f9bcfe28a0bb558ea7c4bb97e484fb35ae3
Parents: c7d591f
Author: tledkov-gridgain <tled...@gridgain.com>
Authored: Thu Sep 21 17:51:26 2017 +0300
Committer: devozerov <ppoze...@gmail.com>
Committed: Thu Sep 21 17:51:26 2017 +0300

----------------------------------------------------------------------
 .../jdbc/thin/JdbcThinErrorsSelfTest.java       | 31 ++++++++++++++++++++
 .../internal/jdbc/thin/JdbcThinStatement.java   |  7 +++--
 2 files changed, 36 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/f8ae8f9b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinErrorsSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinErrorsSelfTest.java
 
b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinErrorsSelfTest.java
index afd06ed..db70f3be 100644
--- 
a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinErrorsSelfTest.java
+++ 
b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinErrorsSelfTest.java
@@ -17,9 +17,11 @@
 
 package org.apache.ignite.jdbc.thin;
 
+import java.sql.BatchUpdateException;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
+import java.sql.Statement;
 import org.apache.ignite.jdbc.JdbcErrorsAbstractSelfTest;
 import org.apache.ignite.lang.IgniteCallable;
 
@@ -74,4 +76,33 @@ public class JdbcThinErrorsSelfTest extends 
JdbcErrorsAbstractSelfTest {
             }
         }, "0700E");
     }
+
+    /**
+     * Test error code for the case when error is caused on batch execution.
+     * @throws SQLException if failed.
+     */
+    @SuppressWarnings("MagicConstant")
+    public void testBatchUpdateException() throws SQLException {
+        try (final Connection conn = getConnection()) {
+            try (Statement stmt = conn.createStatement()) {
+                stmt.executeUpdate("CREATE TABLE test (id int primary key, val 
varchar)");
+
+                stmt.addBatch("insert into test (id, val) values (1, 'val1')");
+                stmt.addBatch("insert into test (id, val) values (2, 'val2')");
+                stmt.addBatch("insert into test (id1, val1) values (3, 
'val3')");
+
+                stmt.executeBatch();
+
+                fail("BatchUpdateException is expected");
+            }
+            catch (BatchUpdateException e) {
+                assertEquals(2, e.getUpdateCounts().length);
+
+                for (int updCnt : e.getUpdateCounts())
+                    assertEquals(1, updCnt);
+
+                assertEquals("42000", e.getSQLState());
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/f8ae8f9b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java
index 6ab50de..8e096c8 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java
@@ -27,6 +27,7 @@ import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.ignite.cache.query.SqlQuery;
+import org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode;
 import org.apache.ignite.internal.processors.odbc.SqlStateCode;
 import org.apache.ignite.internal.processors.odbc.ClientListenerResponse;
 import org.apache.ignite.internal.processors.odbc.jdbc.JdbcBatchExecuteRequest;
@@ -371,8 +372,10 @@ public class JdbcThinStatement implements Statement {
         try {
             JdbcBatchExecuteResult res = conn.sendRequest(new 
JdbcBatchExecuteRequest(conn.getSchema(), batch));
 
-            if (res.errorCode() != ClientListenerResponse.STATUS_SUCCESS)
-                throw new BatchUpdateException(res.errorMessage(), null, 
res.errorCode(), res.updateCounts());
+            if (res.errorCode() != ClientListenerResponse.STATUS_SUCCESS) {
+                throw new BatchUpdateException(res.errorMessage(), 
IgniteQueryErrorCode.codeToSqlState(res.errorCode()),
+                    res.errorCode(), res.updateCounts());
+            }
 
             return res.updateCounts();
         }

Reply via email to