Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating multiple transactions in a single hibernate session
    primarykey
    data
    text
    <p>I have created a Quartz Job which runs in background in my JBoss server and is responsible for updating some statistical data at regular interval (coupled with some database flags)</p> <p>To load and persist the I am using Hibernate 4. Everything works fine except one hick up.</p> <p>The entire thread i.e. Job is wrapped in a Single transaction which over the period of time (as the amount of data increases) becomes huge and worry some. I am trying to break this single large transaction into multiple small ones, such that each transaction process only a sub group of data.</p> <p>Problem: I tried very lamely to wrap a code into a loop and start/end transaction at start/end of the loop. As I expected it didn't work. I have been looking around various forums to figure out a solution but have not come across anything that indicates managing multiple transaction in a single session (where in only 1 transaction will be active at a time).</p> <p>I am relatively new to hibernate and would appreciate any help that points me to a direction on achieving this. </p> <p>Update: Adding code demonstrate what I am trying to achieve and mean when I say breaking into multiple transaction. And stack trace when this is executed.</p> <pre><code> log.info("Starting Calculation Job."); List&lt;GroupModel&gt; groups = Collections.emptyList(); DAOFactory hibDaoFactory = null; try { hibDaoFactory = DAOFactory.hibernate(); hibDaoFactory.beginTransaction(); OrganizationDao groupDao = hibDaoFactory.getGroupDao(); groups = groupDao.findAll(); hibDaoFactory.commitTransaction(); } catch (Exception ex) { hibDaoFactory.rollbackTransaction(); log.error("Error in transaction", ex); } try { hibDaoFactory = DAOFactory.hibernate(); StatsDao statsDao = hibDaoFactory.getStatsDao(); StatsScaledValuesDao statsScaledDao = hibDaoFactory.getStatsScaledValuesDao(); for (GroupModel grp : groups) { try { hibDaoFactory.beginTransaction(); log.info("Performing computation for Group " + grp.getName() + " [" + grp.getId() + "]"); List&lt;Stats&gt; statsDetail = statsDao.loadStatsGroup(grp.getId()); // Coputing Steps here for (Entry origEntry : statsEntries) { entry.setCalculatedItem1(origEntry.getCalculatedItem1()); entry.setCalculatedItem2(origEntry.getCalculatedItem2()); entry.setCalculatedItem3(origEntry.getCalculatedItem3()); StatsDetailsScaledValues scValues = entry.getScaledValues(); if (scValues == null) { scValues = new StatsDetailsScaledValues(); scValues.setId(origEntry.getScrEntryId()); scValues.setValues(origEntry.getScaledValues()); } else { scValues.setValues(origEntry.getScaledValues()); } statsScaledDao.makePersistent(scValues); } hibDaoFactory.commitTransaction(); } catch (Exception ex) { hibDaoFactory.rollbackTransaction(); log.error("Error in transaction", ex); } finally { } } } catch (Exception ex) { log.error("Error", ex); } finally { } log.info("Job Complete."); </code></pre> <p>Following is the exception stacktrace I am getting upon execution of this Job</p> <pre><code>org.hibernate.SessionException: Session is closed! at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:127) at org.hibernate.internal.SessionImpl.createCriteria(SessionImpl.java:1555) at sun.reflect.GeneratedMethodAccessor469.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) at $Proxy308.createCriteria(Unknown Source) at com.blueoptima.cs.dao.impl.hibernate.GenericHibernateDao.findByCriteria(GenericHibernateDao.java:132) at com.blueoptima.cs.dao.impl.hibernate.ScrStatsManagementHibernateDao.loadStatsEntriesForOrg(ScrStatsManagementHibernateDao.java:22) ... 3 more </code></pre> <p>To my understanding from what I have read so far about Hibernate, sessions and transactions. It seems that when a session is created it is attached to the thread and lives through out the threads life or when commit or rollback is called. Thus, when the first transaction is committed the session is being closed and is unavailable for the rest of the threads life.</p> <p>My question remains: How can we have multiple transactions in a single session?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload