@@ -43,23 +43,11 @@ public int execute(
4343 final SharedSessionContractImplementor session = executionContext .getSession ();
4444 session .autoFlushIfRequired ( jdbcMutation .getAffectedTableNames () );
4545
46- final LogicalConnectionImplementor logicalConnection = session
47- .getJdbcCoordinator ()
48- .getLogicalConnection ();
46+ final LogicalConnectionImplementor logicalConnection =
47+ session .getJdbcCoordinator ().getLogicalConnection ();
4948
5049 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 );
6351 try {
6452 // prepare the query
6553 final PreparedStatement preparedStatement = statementCreator .apply ( finalSql );
@@ -83,9 +71,10 @@ public int execute(
8371
8472 session .getEventListenerManager ().jdbcExecuteStatementStart ();
8573 final EventMonitor eventMonitor = session .getEventMonitor ();
86- final DiagnosticEvent jdbcPreparedStatementExecutionEvent = eventMonitor .beginJdbcPreparedStatementExecutionEvent ();
74+ final DiagnosticEvent jdbcPreparedStatementExecutionEvent =
75+ eventMonitor .beginJdbcPreparedStatementExecutionEvent ();
8776 try {
88- int rows = preparedStatement .executeUpdate ();
77+ final int rows = preparedStatement .executeUpdate ();
8978 expectationCheck .accept ( rows , preparedStatement );
9079 return rows ;
9180 }
@@ -99,33 +88,49 @@ public int execute(
9988 }
10089 }
10190 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 );
11992 }
12093 finally {
12194 executionContext .afterStatement ( logicalConnection );
12295 }
12396 }
12497
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+
125130 private static boolean constraintNameMatches (String uniqueConstraintNameThatMayFail , String violatedConstraintName ) {
126131 return uniqueConstraintNameThatMayFail .isEmpty ()
127- || uniqueConstraintNameThatMayFail .equalsIgnoreCase (violatedConstraintName )
132+ || uniqueConstraintNameThatMayFail .equalsIgnoreCase ( violatedConstraintName )
128133 || violatedConstraintName != null && violatedConstraintName .indexOf ('.' ) > 0
129- && uniqueConstraintNameThatMayFail .equalsIgnoreCase (violatedConstraintName .substring (violatedConstraintName .lastIndexOf ('.' ) + 1 ));
134+ && uniqueConstraintNameThatMayFail .equalsIgnoreCase ( violatedConstraintName .substring (violatedConstraintName .lastIndexOf ('.' ) + 1 ) );
130135 }
131136}
0 commit comments