Skip to content

Commit 6357b7c

Browse files
committed
1 parent 7d4ee7c commit 6357b7c

File tree

4 files changed

+771
-304
lines changed

4 files changed

+771
-304
lines changed

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/AbstractAssociationOperator.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
import org.babyfish.jimmer.sql.runtime.*;
1010
import org.jetbrains.annotations.Nullable;
1111

12-
import java.sql.BatchUpdateException;
13-
import java.sql.Connection;
14-
import java.sql.PreparedStatement;
15-
import java.sql.SQLException;
12+
import java.sql.*;
1613
import java.util.ArrayList;
1714
import java.util.Collection;
1815
import java.util.Collections;
@@ -106,9 +103,24 @@ int[] executeImpl(
106103
variables,
107104
sqlTuple.get_3(),
108105
ExecutionPurpose.MUTATE,
109-
null,
106+
(ex, args) -> {
107+
if (ex instanceof SQLException) {
108+
return exceptionTranslator.apply((SQLException) ex, null);
109+
}
110+
return ex;
111+
},
110112
Connection::prepareStatement,
111-
(stmt, args) -> stmt.executeUpdate()
113+
(stmt, args) -> {
114+
Savepoint savepoint = SavepointManager.setIfNeeded(args.con, sqlClient);
115+
try {
116+
return stmt.executeUpdate();
117+
} catch (SQLException ex) {
118+
SavepointManager.rollback(stmt::getConnection, savepoint);
119+
throw ex;
120+
} finally {
121+
SavepointManager.release(stmt::getConnection, savepoint);
122+
}
123+
}
112124
)
113125
);
114126
}

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/MiddleTableOperator.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class MiddleTableOperator extends AbstractAssociationOperator {
2929

3030
private static final int[] EMPTY_ROW_COUNTS = new int[0];
3131

32+
private static final int[] SINGLE_ERROR_ROW_COUNTS = new int[] {-1};
33+
3234
private final MutationPath path;
3335

3436
private final ExceptionTranslator<Exception> exceptionTranslator;
@@ -996,21 +998,26 @@ private Exception translateConnectException(
996998
Collection<Tuple2<Object, Object>> idTuples
997999
) {
9981000
String state = ex.getSQLState();
999-
if (state == null || !state.startsWith("23") || !(ex instanceof BatchUpdateException)) {
1001+
if (state == null || !state.startsWith("23")) {
10001002
return convertFinalException(ex, ctx);
10011003
}
1002-
BatchUpdateException bue = (BatchUpdateException) ex;
1004+
int[] affectedRowCounts;
1005+
if (ex instanceof BatchUpdateException) {
1006+
affectedRowCounts = ((BatchUpdateException)ex).getUpdateCounts();
1007+
} else {
1008+
affectedRowCounts = SINGLE_ERROR_ROW_COUNTS;
1009+
}
10031010
MiddleTableInvestigator investigator = new MiddleTableInvestigator(
1004-
bue,
1005-
bue.getUpdateCounts(),
1011+
ex,
1012+
affectedRowCounts,
10061013
Investigators.toInvestigatorSqlClient(sqlClient, ctx),
10071014
con,
10081015
path,
10091016
idTuples
10101017
);
10111018
Exception investigatedException = investigator.investigate();
10121019
if (investigatedException == null) {
1013-
investigatedException = bue;
1020+
investigatedException = ex;
10141021
}
10151022
return convertFinalException(investigatedException, ctx);
10161023
}

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/runtime/DefaultExecutor.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ private static void setParameters(
4646
}
4747
}
4848

49+
@SuppressWarnings("unchecked")
4950
@Override
5051
public <R> R execute(@NotNull Args<R> args) {
5152
String sql = args.sql;
@@ -59,19 +60,22 @@ public <R> R execute(@NotNull Args<R> args) {
5960
return args.block.apply(stmt, args);
6061
} catch (Exception ex) {
6162
ExceptionTranslator<Exception> exceptionTranslator =
62-
args.sqlClient().getExceptionTranslator();
63-
if (exceptionTranslator != null) {
64-
ex = exceptionTranslator.translate(ex, args);
63+
(ExceptionTranslator<Exception>) args.getExceptionTranslator();
64+
Exception translatedException;
65+
if (exceptionTranslator == null) {
66+
translatedException = ex;
67+
} else {
68+
translatedException = exceptionTranslator.translate(ex, args);
6569
}
66-
if (ex instanceof RuntimeException) {
67-
throw (RuntimeException) ex;
70+
if (translatedException instanceof RuntimeException) {
71+
throw (RuntimeException) translatedException;
6872
}
6973
throw new ExecutionException(
7074
"Cannot execute SQL statement: " +
7175
sql +
7276
", variables: " +
7377
variables,
74-
ex
78+
translatedException
7579
);
7680
}
7781
}

0 commit comments

Comments
 (0)