Skip to content

Commit b4dcb36

Browse files
committed
Align currentStatelessSession() implementation with currentSession()
See gh-7184
1 parent 252bb8d commit b4dcb36

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

spring-orm/src/main/java/org/springframework/orm/jpa/hibernate/SpringSessionContext.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,6 @@ else if (value instanceof EntityManagerHolder entityManagerHolder) {
171171
* @return the current StatelessSession
172172
*/
173173
public static StatelessSession currentStatelessSession(SessionFactory sessionFactory) {
174-
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
175-
throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
176-
}
177174
Object value = TransactionSynchronizationManager.getResource(sessionFactory);
178175
if (value instanceof StatelessSession statelessSession) {
179176
return statelessSession;
@@ -185,14 +182,20 @@ public static StatelessSession currentStatelessSession(SessionFactory sessionFac
185182
}
186183
holder = sessionHolder;
187184
}
188-
StatelessSession session = sessionFactory.openStatelessSession(determineConnection(sessionFactory, holder));
189-
if (holder != null) {
190-
holder.setStatelessSession(session);
185+
186+
if (TransactionSynchronizationManager.isSynchronizationActive()) {
187+
StatelessSession session = sessionFactory.openStatelessSession(determineConnection(sessionFactory, holder));
188+
if (holder != null) {
189+
holder.setStatelessSession(session);
190+
}
191+
else {
192+
bindSessionHolder(sessionFactory, new SessionHolder(session));
193+
}
194+
return session;
191195
}
192196
else {
193-
bindSessionHolder(sessionFactory, new SessionHolder(session));
197+
throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
194198
}
195-
return session;
196199
}
197200

198201
private static void bindSessionHolder(SessionFactory sessionFactory, SessionHolder holder) {

0 commit comments

Comments
 (0)