4545/**
4646 * @author Andrea Boriero
4747 */
48- public class ExceptionConverterImpl implements ExceptionConverter {
48+ class ExceptionConverterImpl implements ExceptionConverter {
4949
5050 private final SharedSessionContractImplementor session ;
5151 private final boolean isJpaBootstrap ;
5252
53- public ExceptionConverterImpl (SharedSessionContractImplementor session ) {
53+ ExceptionConverterImpl (SharedSessionContractImplementor session ) {
5454 this .session = session ;
5555 isJpaBootstrap = session .getFactory ().getSessionFactoryOptions ().isJpaBootstrap ();
5656 }
@@ -78,46 +78,46 @@ public RuntimeException convertCommitException(RuntimeException exception) {
7878 @ Override
7979 public RuntimeException convert (HibernateException exception , LockOptions lockOptions ) {
8080 if ( exception instanceof StaleStateException staleStateException ) {
81- final PersistenceException converted = wrapStaleStateException ( staleStateException );
81+ final var converted = wrapStaleStateException ( staleStateException );
8282 rollbackIfNecessary ( converted );
8383 return converted ;
8484 }
8585 else if ( exception instanceof org .hibernate .PessimisticLockException pessimisticLockException ) {
86- final PersistenceException converted = wrapLockException ( pessimisticLockException , lockOptions );
86+ final var converted = wrapLockException ( pessimisticLockException , lockOptions );
8787 rollbackIfNecessary ( converted );
8888 return converted ;
8989 }
9090 else if ( exception instanceof LockingStrategyException lockingStrategyException ) {
91- final PersistenceException converted = wrapLockException ( lockingStrategyException , lockOptions );
91+ final var converted = wrapLockException ( lockingStrategyException , lockOptions );
9292 rollbackIfNecessary ( converted );
9393 return converted ;
9494 }
9595 else if ( exception instanceof SnapshotIsolationException ) {
9696 return new OptimisticLockException ( exception .getMessage (), exception );
9797 }
9898 else if ( exception instanceof org .hibernate .QueryTimeoutException ) {
99- final QueryTimeoutException converted = new QueryTimeoutException ( exception .getMessage (), exception );
99+ final var converted = new QueryTimeoutException ( exception .getMessage (), exception );
100100 rollbackIfNecessary ( converted );
101101 return converted ;
102102 }
103103 else if ( exception instanceof ObjectNotFoundException ) {
104- final EntityNotFoundException converted = new EntityNotFoundException ( exception .getMessage (), exception );
104+ final var converted = new EntityNotFoundException ( exception .getMessage (), exception );
105105 rollbackIfNecessary ( converted );
106106 return converted ;
107107 }
108108 else if ( exception instanceof org .hibernate .NonUniqueObjectException
109109 || exception instanceof PersistentObjectException ) {
110- final EntityExistsException converted = new EntityExistsException ( exception .getMessage (), exception );
110+ final var converted = new EntityExistsException ( exception .getMessage (), exception );
111111 rollbackIfNecessary ( converted );
112112 return converted ;
113113 }
114114 else if ( exception instanceof org .hibernate .NonUniqueResultException ) {
115- final NonUniqueResultException converted = new NonUniqueResultException ( exception .getMessage (), exception );
115+ final var converted = new NonUniqueResultException ( exception .getMessage (), exception );
116116 rollbackIfNecessary ( converted );
117117 return converted ;
118118 }
119119 else if ( exception instanceof UnresolvableObjectException ) {
120- final EntityNotFoundException converted = new EntityNotFoundException ( exception .getMessage (), exception );
120+ final var converted = new EntityNotFoundException ( exception .getMessage (), exception );
121121 rollbackIfNecessary ( converted );
122122 return converted ;
123123 }
@@ -141,7 +141,7 @@ else if ( exception instanceof TransientObjectException ) {
141141 return new IllegalStateException ( exception );
142142 }
143143 else if ( exception instanceof TransactionSerializationException ) {
144- final PersistenceException converted = new RollbackException ( exception .getMessage (), exception );
144+ final var converted = new RollbackException ( exception .getMessage (), exception );
145145 rollbackIfNecessary ( converted );
146146 return converted ;
147147 }
@@ -184,48 +184,52 @@ public JDBCException convert(SQLException e, String message) {
184184 }
185185
186186 protected PersistenceException wrapStaleStateException (StaleStateException exception ) {
187+ final String message = exception .getMessage ();
187188 if ( exception instanceof StaleObjectStateException staleStateException ) {
188189 final Object identifier = staleStateException .getIdentifier ();
189190 final String entityName = staleStateException .getEntityName ();
190191 if ( identifier != null ) {
191192 try {
192193 final Object entity = session .internalLoad ( entityName , identifier , false , true );
193194 if ( entity instanceof Serializable ) { // avoid some user errors regarding boundary crossing
194- return new OptimisticLockException ( exception . getMessage () , exception , entity );
195+ return new OptimisticLockException ( message , exception , entity );
195196 }
196197 }
197198 catch (EntityNotFoundException entityNotFoundException ) {
198199 // swallow it;
199200 }
200201 }
201202 }
202- return new OptimisticLockException ( exception . getMessage () , exception );
203+ return new OptimisticLockException ( message , exception );
203204 }
204205
205206 protected PersistenceException wrapLockException (LockingStrategyException exception , LockOptions lockOptions ) {
207+ final String message = exception .getMessage ();
208+ final Object entity = exception .getEntity ();
206209 if ( exception instanceof OptimisticEntityLockException lockException ) {
207- return new OptimisticLockException ( lockException . getMessage () , lockException , lockException . getEntity () );
210+ return new OptimisticLockException ( message , lockException , entity );
208211 }
209212 else if ( exception instanceof PessimisticEntityLockException lockException ) {
210213 // assume lock timeout occurred if a timeout or NO WAIT was specified
211214 return lockOptions != null && lockOptions .getTimeout ().milliseconds () > -1
212- ? new LockTimeoutException ( lockException . getMessage () , lockException , lockException . getEntity () )
213- : new PessimisticLockException ( lockException . getMessage () , lockException , lockException . getEntity () );
215+ ? new LockTimeoutException ( message , lockException , entity )
216+ : new PessimisticLockException ( message , lockException , entity );
214217 }
215218 else {
216219 throw new AssertionFailure ( "Unrecognized exception type" );
217220 }
218221 }
219222
220223 protected PersistenceException wrapLockException (org .hibernate .PessimisticLockException exception , LockOptions lockOptions ) {
224+ final String message = exception .getMessage ();
221225 if ( exception instanceof org .hibernate .exception .LockTimeoutException ) {
222- return new LockTimeoutException ( exception . getMessage () , exception );
226+ return new LockTimeoutException ( message , exception );
223227 }
224228 else {
225229 // assume lock timeout occurred if a timeout or NO WAIT was specified
226230 return lockOptions != null && lockOptions .getTimeout ().milliseconds () > -1
227- ? new LockTimeoutException ( exception . getMessage () , exception )
228- : new PessimisticLockException ( exception . getMessage () , exception );
231+ ? new LockTimeoutException ( message , exception )
232+ : new PessimisticLockException ( message , exception );
229233 }
230234 }
231235
0 commit comments