@@ -43,23 +43,11 @@ public int execute(
43
43
final SharedSessionContractImplementor session = executionContext .getSession ();
44
44
session .autoFlushIfRequired ( jdbcMutation .getAffectedTableNames () );
45
45
46
- final LogicalConnectionImplementor logicalConnection = session
47
- .getJdbcCoordinator ()
48
- .getLogicalConnection ();
46
+ final LogicalConnectionImplementor logicalConnection =
47
+ session .getJdbcCoordinator ().getLogicalConnection ();
49
48
50
49
final JdbcServices jdbcServices = session .getJdbcServices ();
51
- final QueryOptions queryOptions = executionContext .getQueryOptions ();
52
- final String finalSql ;
53
- if ( queryOptions == null ) {
54
- finalSql = jdbcMutation .getSqlString ();
55
- }
56
- else {
57
- finalSql = jdbcServices .getDialect ().addSqlHintOrComment (
58
- jdbcMutation .getSqlString (),
59
- queryOptions ,
60
- executionContext .getSession ().getFactory ().getSessionFactoryOptions ().isCommentsEnabled ()
61
- );
62
- }
50
+ final String finalSql = applyOptions ( jdbcMutation , executionContext , jdbcServices );
63
51
try {
64
52
// prepare the query
65
53
final PreparedStatement preparedStatement = statementCreator .apply ( finalSql );
@@ -83,9 +71,10 @@ public int execute(
83
71
84
72
session .getEventListenerManager ().jdbcExecuteStatementStart ();
85
73
final EventMonitor eventMonitor = session .getEventMonitor ();
86
- final DiagnosticEvent jdbcPreparedStatementExecutionEvent = eventMonitor .beginJdbcPreparedStatementExecutionEvent ();
74
+ final DiagnosticEvent jdbcPreparedStatementExecutionEvent =
75
+ eventMonitor .beginJdbcPreparedStatementExecutionEvent ();
87
76
try {
88
- int rows = preparedStatement .executeUpdate ();
77
+ final int rows = preparedStatement .executeUpdate ();
89
78
expectationCheck .accept ( rows , preparedStatement );
90
79
return rows ;
91
80
}
@@ -99,33 +88,49 @@ public int execute(
99
88
}
100
89
}
101
90
catch (SQLException e ) {
102
- final JDBCException exception = jdbcServices .getSqlExceptionHelper ().convert (
103
- e ,
104
- "JDBC exception executing SQL [" + finalSql + "]"
105
- );
106
- if ( exception instanceof ConstraintViolationException constraintViolationException && jdbcMutation instanceof JdbcOperationQueryInsert ) {
107
- if ( constraintViolationException .getKind () == ConstraintViolationException .ConstraintKind .UNIQUE ) {
108
- final JdbcOperationQueryInsert jdbcInsert = (JdbcOperationQueryInsert ) jdbcMutation ;
109
- final String uniqueConstraintNameThatMayFail = jdbcInsert .getUniqueConstraintNameThatMayFail ();
110
- if ( uniqueConstraintNameThatMayFail != null ) {
111
- final String violatedConstraintName = constraintViolationException .getConstraintName ();
112
- if ( constraintNameMatches ( uniqueConstraintNameThatMayFail , violatedConstraintName ) ) {
113
- return 0 ;
114
- }
115
- }
116
- }
117
- }
118
- throw exception ;
91
+ return handleException ( jdbcMutation , e , jdbcServices , finalSql );
119
92
}
120
93
finally {
121
94
executionContext .afterStatement ( logicalConnection );
122
95
}
123
96
}
124
97
98
+ private static int handleException (
99
+ JdbcOperationQueryMutation jdbcMutation , SQLException sqle , JdbcServices jdbcServices , String finalSql ) {
100
+ final JDBCException exception =
101
+ jdbcServices .getSqlExceptionHelper ()
102
+ .convert ( sqle , "JDBC exception executing SQL [" + finalSql + "]" );
103
+ if ( exception instanceof ConstraintViolationException constraintViolationException
104
+ && jdbcMutation instanceof JdbcOperationQueryInsert jdbcInsert ) {
105
+ if ( constraintViolationException .getKind () == ConstraintViolationException .ConstraintKind .UNIQUE ) {
106
+ final String uniqueConstraintNameThatMayFail = jdbcInsert .getUniqueConstraintNameThatMayFail ();
107
+ if ( uniqueConstraintNameThatMayFail != null ) {
108
+ final String violatedConstraintName = constraintViolationException .getConstraintName ();
109
+ if ( constraintNameMatches ( uniqueConstraintNameThatMayFail , violatedConstraintName ) ) {
110
+ return 0 ;
111
+ }
112
+ }
113
+ }
114
+ }
115
+ throw exception ;
116
+ }
117
+
118
+ private static String applyOptions (
119
+ JdbcOperationQueryMutation jdbcMutation , ExecutionContext executionContext , JdbcServices jdbcServices ) {
120
+ final QueryOptions queryOptions = executionContext .getQueryOptions ();
121
+ return queryOptions == null
122
+ ? jdbcMutation .getSqlString ()
123
+ : jdbcServices .getDialect ().addSqlHintOrComment (
124
+ jdbcMutation .getSqlString (),
125
+ queryOptions ,
126
+ executionContext .getSession ().getFactory ().getSessionFactoryOptions ().isCommentsEnabled ()
127
+ );
128
+ }
129
+
125
130
private static boolean constraintNameMatches (String uniqueConstraintNameThatMayFail , String violatedConstraintName ) {
126
131
return uniqueConstraintNameThatMayFail .isEmpty ()
127
- || uniqueConstraintNameThatMayFail .equalsIgnoreCase (violatedConstraintName )
132
+ || uniqueConstraintNameThatMayFail .equalsIgnoreCase ( violatedConstraintName )
128
133
|| violatedConstraintName != null && violatedConstraintName .indexOf ('.' ) > 0
129
- && uniqueConstraintNameThatMayFail .equalsIgnoreCase (violatedConstraintName .substring (violatedConstraintName .lastIndexOf ('.' ) + 1 ));
134
+ && uniqueConstraintNameThatMayFail .equalsIgnoreCase ( violatedConstraintName .substring (violatedConstraintName .lastIndexOf ('.' ) + 1 ) );
130
135
}
131
136
}
0 commit comments