Package org.hibernate

Interface SharedSessionBuilder

All Superinterfaces:
CommonBuilder, CommonSharedBuilder, SessionBuilder
All Known Subinterfaces:
SharedSessionBuilderImplementor
All Known Implementing Classes:
AbstractDelegatingSharedSessionBuilder

public interface SharedSessionBuilder extends SessionBuilder, CommonSharedBuilder
Allows creation of a child Session which shares some options with another pre-existing parent session. Each session has its own isolated persistence context, and entity instances must not be shared between parent and child sessions.

When resource-local transaction management is used:

  • by default, each session executes with its own dedicated JDBC connection and therefore has its own isolated transaction, but
  • calling the connection() method specifies that the connection, and therefore also the JDBC transaction, should be shared from parent to child.

 try (var childSession
          = session.sessionWithOptions()
                  .connection() // share the JDBC connection
                  .cacheMode(CacheMode.IGNORE)
                  .openSession()) {
     ...
 }
 

On the other hand, when JTA transaction management is used, all sessions execute within the same transaction. Typically, connection sharing is handled automatically by the JTA-enabled DataSource.

See Also: