i have a somewhat unique requirement in that i need to perform some batch processing within a process that also has normal business logic. ive searched the forums countless times for other scenarios using the StatelessSession but i dont see anything particularly helpful..thus im asking in a new thread. i apologize in advance for the long comment but im trying to explain the scenario properly.
so basically the process flow is as follows:
1. start of process
2. do some work
3. do a lot of batch inserts/updates/deletes
4. do some more work
5. end of process
in step 2 i load and persist some entities with relationships and collections.
in step 3 i am persisting millions of POJO value objects.
in step 4 i am working again with entities with relationships and collections
i want the whole process to execute atomically within a transaction (i.e. either all steps 1-5 succeed or they fail)
in order to perform steps 2 and 4 i want to use a regular session and for step 3 i would like to use a StatelessSession
what im trying to do is piggyback a StatelessSession on a Session using the following technique
Code:
//start of process
Session session = sf.getCurrentSession();
session.beginTransaction();
//do work in step 2 using session
StatelessSession statelessSession = sf.openStatelessSession(sf.getCurrentSession().connection());
//do work in step 3 using statelessSession
//do work in step 4 using session
sf.getCurrentSession().getTransaction().commit();
//end of process
unfortunately, this doesnt seem to work. the only way i can get it to work is if i invoke beginTransaction() on the statelessSession before step 3 and then call commit() on the statelessSession after step 3. essentially beginning and committing two transactions on the same jdbc connection. im confused as to why this is needed? when i wrap step 3 in its own transaction using the StatelessSession interface, i get the debug output listed below.....its as if two seperate JDBC connections are being used. is a nested subtransaction actually occurring (i.e. where a committed inner transaction is only permanent if the outer transaction commits)? for me a single flat transaction would be best (which is why i tried to piggyback the connection).
is there some way to properly accomplish what im trying to do in a fully atomic way?
Code:
18 Jun 2008 02:15:33,188 DEBUG TransactionAspect - Beginning Session transaction
18 Jun 2008 02:15:33,668 DEBUG JDBCTransaction - begin
18 Jun 2008 02:15:33,670 DEBUG JDBCTransaction - current autocommit status: false
18 Jun 2008 02:15:33,671 DEBUG HibernateXpressfeedDAO - Openning new StatelessSession on Session connection
18 Jun 2008 02:15:33,787 DEBUG HibernateXpressfeedDAO - Beginning StatelessSession transaction
18 Jun 2008 02:15:33,788 DEBUG JDBCTransaction - begin
18 Jun 2008 02:15:33,788 DEBUG JDBCTransaction - current autocommit status: false
18 Jun 2008 02:15:33,894 DEBUG HibernateXpressfeedDAO - Committing StatelessSession transaction
18 Jun 2008 02:15:33,895 DEBUG JDBCTransaction - commit
18 Jun 2008 02:15:33,902 DEBUG JDBCTransaction - committed JDBC Connection
18 Jun 2008 02:15:33,903 DEBUG TransactionAspect - Committing Session transaction
18 Jun 2008 02:15:33,903 DEBUG JDBCTransaction - commit
18 Jun 2008 02:15:33,905 DEBUG JDBCTransaction - committed JDBC Connection
in case anyone is wondering, the process is executed by a single thread, im using hibernate 3, mysql innodb and the following properties:
hibernate.jdbc.batch_size=80
hibernate.cache.use_second_level_cache=false
hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
hibernate.current_session_context_class=thread
hibernate.connection.autocommit=false
thanks in advance