com.atlassian.confluence.core.persistence.hibernate
Class HibernateSessionManager

java.lang.Object
  extended by com.atlassian.confluence.core.persistence.hibernate.HibernateSessionManager

public class HibernateSessionManager
extends java.lang.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.

Since:
3.5
See Also:
Session, SessionFactoryUtils.getSession(SessionFactory, boolean)

Constructor Summary
HibernateSessionManager(net.sf.hibernate.SessionFactory sessionFactory, org.springframework.transaction.PlatformTransactionManager transactionManager)
           
 
Method Summary
 void evict(java.lang.Object object)
          Removes the object and its dependents from the active session.
<I,O> java.lang.Iterable<O>
executeWithSessionClearing(java.lang.Iterable<I> input, int batchSize, int expectedTotal, com.google.common.base.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.
<I,O> java.lang.Iterable<O>
executeWithSessionFlushAndClear(java.lang.Iterable<I> input, int batchSize, int expectedTotal, com.google.common.base.Function<I,O> task)
          Reuses the current transaction, to avoid nested transaction issues.
 net.sf.hibernate.Session getSession()
          Returns the current session.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateSessionManager

public HibernateSessionManager(net.sf.hibernate.SessionFactory sessionFactory,
                               org.springframework.transaction.PlatformTransactionManager transactionManager)
Method Detail

getSession

public net.sf.hibernate.Session getSession()
Returns the current session.

Throws:
java.lang.IllegalStateException - if there is no active session

evict

public void evict(java.lang.Object object)
Removes the object and its dependents from the active session. Any un-flushed changes will be lost.

This method should be used when processing a large number of Hibernate objects in a read-only fashion.

Throws:
java.lang.IllegalStateException - if there is no active session
org.springframework.dao.DataAccessException - if there is a problem evicting the object
See Also:
Session.evict(Object)

executeWithSessionClearing

public <I,O> java.lang.Iterable<O> executeWithSessionClearing(java.lang.Iterable<I> input,
                                                              int batchSize,
                                                              int expectedTotal,
                                                              com.google.common.base.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:
java.lang.IllegalStateException - if there is no active session
org.springframework.dao.DataAccessException - if there is a problem committing changes to the database

executeWithSessionFlushAndClear

public <I,O> java.lang.Iterable<O> executeWithSessionFlushAndClear(java.lang.Iterable<I> input,
                                                                   int batchSize,
                                                                   int expectedTotal,
                                                                   com.google.common.base.Function<I,O> task)
Reuses the current transaction, to avoid nested transaction issues.



Copyright © 2003-2014 Atlassian. All Rights Reserved.