@@ -150,8 +150,8 @@ public static <T> T performWork(
150150 String attributeName ) {
151151 SharedSessionContractImplementor session = interceptor .getLinkedSession ();
152152
153- boolean isTempSession = false ;
154- boolean isJta = false ;
153+ final boolean isTempSession ;
154+ final boolean isJta ;
155155
156156 // first figure out which Session to use
157157 if ( session == null ) {
@@ -160,7 +160,7 @@ public static <T> T performWork(
160160 isTempSession = true ;
161161 }
162162 else {
163- throwLazyInitializationException ( Cause .NO_SESSION , entityName , attributeName );
163+ throw createLazyInitializationException ( Cause .NO_SESSION , entityName , attributeName );
164164 }
165165 }
166166 else if ( !session .isOpen () ) {
@@ -169,7 +169,7 @@ else if ( !session.isOpen() ) {
169169 isTempSession = true ;
170170 }
171171 else {
172- throwLazyInitializationException ( Cause .CLOSED_SESSION , entityName , attributeName );
172+ throw createLazyInitializationException ( Cause .CLOSED_SESSION , entityName , attributeName );
173173 }
174174 }
175175 else if ( !session .isConnected () ) {
@@ -178,9 +178,12 @@ else if ( !session.isConnected() ) {
178178 isTempSession = true ;
179179 }
180180 else {
181- throwLazyInitializationException ( Cause .DISCONNECTED_SESSION , entityName , attributeName );
181+ throw createLazyInitializationException ( Cause .DISCONNECTED_SESSION , entityName , attributeName );
182182 }
183183 }
184+ else {
185+ isTempSession = false ;
186+ }
184187
185188 // If we are using a temporary Session, begin a transaction if necessary
186189 if ( isTempSession ) {
@@ -198,6 +201,9 @@ else if ( !session.isConnected() ) {
198201 session .beginTransaction ();
199202 }
200203 }
204+ else {
205+ isJta = false ;
206+ }
201207
202208 try {
203209 // do the actual work
@@ -238,29 +244,13 @@ enum Cause {
238244 NO_SF_UUID
239245 }
240246
241- private static void throwLazyInitializationException (Cause cause , String entityName , String attributeName ) {
242- final String reason ;
243- switch ( cause ) {
244- case NO_SESSION : {
245- reason = "no session and settings disallow loading outside the Session" ;
246- break ;
247- }
248- case CLOSED_SESSION : {
249- reason = "session is closed and settings disallow loading outside the Session" ;
250- break ;
251- }
252- case DISCONNECTED_SESSION : {
253- reason = "session is disconnected and settings disallow loading outside the Session" ;
254- break ;
255- }
256- case NO_SF_UUID : {
257- reason = "could not determine SessionFactory UUId to create temporary Session for loading" ;
258- break ;
259- }
260- default : {
261- reason = "<should never get here>" ;
262- }
263- }
247+ private static LazyInitializationException createLazyInitializationException (final Cause cause , final String entityName , final String attributeName ) {
248+ final String reason = switch ( cause ) {
249+ case NO_SESSION -> "no session and settings disallow loading outside the Session" ;
250+ case CLOSED_SESSION -> "session is closed and settings disallow loading outside the Session" ;
251+ case DISCONNECTED_SESSION -> "session is disconnected and settings disallow loading outside the Session" ;
252+ case NO_SF_UUID -> "could not determine SessionFactory UUId to create temporary Session for loading" ;
253+ };
264254
265255 final String message = String .format (
266256 Locale .ROOT ,
@@ -270,15 +260,15 @@ private static void throwLazyInitializationException(Cause cause, String entityN
270260 reason
271261 );
272262
273- throw new LazyInitializationException ( message );
263+ return new LazyInitializationException ( message );
274264 }
275265
276266 private static SharedSessionContractImplementor openTemporarySessionForLoading (
277267 BytecodeLazyAttributeInterceptor interceptor ,
278268 String entityName ,
279269 String attributeName ) {
280270 if ( interceptor .getSessionFactoryUuid () == null ) {
281- throwLazyInitializationException ( Cause .NO_SF_UUID , entityName , attributeName );
271+ throw createLazyInitializationException ( Cause .NO_SF_UUID , entityName , attributeName );
282272 }
283273
284274 final SessionFactoryImplementor sf = SessionFactoryRegistry .INSTANCE .getSessionFactory ( interceptor .getSessionFactoryUuid () );
0 commit comments