This is still a problem and source of potential data loss in hibernate 6.1.7. For my case, which is Spring Boot, I’ve implemented this workaround:
@Service
public class StatelessSessionRunner {
private final Session session;
public StatelessSessionRunner(Session session) {
this.session = session;
}
public <T> T runWithStatelessSession(Function<StatelessSession, T> func) {
return session.doReturningWork(connection -> {
try (StatelessSession statelessSession = session.getSessionFactory().openStatelessSession(connection)) {
var result = func.apply(statelessSession);
((SharedSessionContractImplementor) statelessSession).getJdbcCoordinator().executeBatch();
return result;
}
});
}
}
Which allows you to perform some operations using a stateless session within a larger transaction. |