Class HibernateSessionManager5


  • public class HibernateSessionManager5
    extends Object
    Provides methods to manage objects in the current Hibernate Session. Methods will throw IllegalStateException if there is no active session on the executing thread.

    Replacement for the old Hibernate 2 version of HibernateSessionManager due to Hibernate 2 removal.

    Since:
    7.16.0
    • Constructor Detail

      • HibernateSessionManager5

        public HibernateSessionManager5​(org.hibernate.SessionFactory sessionFactory,
                                        org.springframework.transaction.PlatformTransactionManager transactionManager)
    • Method Detail

      • executeThenClearSession

        public <I,​O> Iterable<O> executeThenClearSession​(Iterable<I> input,
                                                               int batchSize,
                                                               int expectedTotal,
                                                               Function<I,​O> task)
        Executes a bulk database update in a way that prevents excessive memory usage, by committing the transaction and clearing the session periodically. The provided task is executed once per input item. The commit and session-clearing is done once per batchSize items.

        WARNING: the session clearing will discard any unflushed or uncommitted changes made higher in the stack. Batch operations using this method should generally be done in a separate thread or otherwise outside the scope of a request.

        Type Parameters:
        I - the type of the input items
        O - the type of the resulting items, which can be Void
        Parameters:
        input - the list of items to process
        task - the task to execute
        batchSize - a transaction will be committed and the session will be cleared after this many input items
        expectedTotal - the total number of items, used for logging. A value of 0 indicates expected total is not known.
        Returns:
        the output of the task
        Throws:
        IllegalStateException - if there is no active session
        org.springframework.dao.DataAccessException - if there is a problem committing changes to the database
      • executeThenFlushAndClearSession

        public <I,​O> Iterable<O> executeThenFlushAndClearSession​(Iterable<I> input,
                                                                       int batchSize,
                                                                       int expectedTotal,
                                                                       Function<I,​O> task)
        Reuses the current transaction, to avoid nested transaction issues.
        Since:
        7.0.1
      • executeThenClearSessionWithoutCommitOrFlush

        public <I,​O> Iterable<O> executeThenClearSessionWithoutCommitOrFlush​(Iterable<I> input,
                                                                                   int batchSize,
                                                                                   int expectedTotal,
                                                                                   Function<I,​O> task)
        Executes a bulk database update in a way that prevents excessive memory usage, by clearing the session (but NOT committing or flushing) periodically. The provided task is executed once per input item. The session-clearing (But no commit) is done once per batchSize items.

        WARNING: the session clearing will discard any unflushed or uncommitted changes made higher in the stack. Also, the existing transaction will not be committed in this method. Use this method only for the operations that do not write to database.

        Type Parameters:
        I - the type of the input items
        O - the type of the resulting items, which can be Void
        Parameters:
        input - the list of items to process
        batchSize - a transaction will be committed and the session will be cleared after this many input items
        expectedTotal - the total number of items, used for logging. A value of 0 indicates expected total is not known.
        task - the task to execute
        Returns:
        the output of the task
        Throws:
        IllegalStateException - if there is no active session
        org.springframework.dao.DataAccessException - if there is a problem committing changes to the database
        Since:
        7.19.12
      • executeThenClearSessionWithoutCommitOrFlush

        public int executeThenClearSessionWithoutCommitOrFlush​(int batchSize,
                                                               int expectedTotal,
                                                               Function<Integer,​Integer> taskExecutor)
        Execute tasks in batches, re-using the existing transaction rather than nesting transactions. WARNING: the session clearing will discard any unflushed or uncommitted changes made higher in the stack. Use this method only for the operations that do not write to database. Also, the existing transaction will not be committed in this method.
        Parameters:
        batchSize - the positive number of tasks to execute in a batch, before any hibernate flush or clears.
        expectedTotal - the positive total number of tasks to be executed.
        taskExecutor - the task executor. The taskExecutor should accept the batchSize to execute, and return how many tasks have been successfully executed.
        Returns:
        the number of tasks which were executed successfully.
      • withNewTransaction

        public boolean withNewTransaction​(Callable<Boolean> callable)
      • getTransactionTemplate

        public org.springframework.transaction.support.TransactionTemplate getTransactionTemplate()