Preconditions.checkArgument(entityClass != null, "The method 'getDao' requires a non null 'entityClass' parameter"); SessionFactory hibernateFactory = hibernateSupport.getHibernateFactory(); Preconditions.checkState(hibernateFactory != null, "The Hibernate SessionFactory is null, please initialize");
@Override public void close() { checkNotClosed(); if (log.isDebugEnabled()) { log.debug("will close " + this); } closed = true; // Rollback and do not create a new transaction rollback0(false); // Now close the current Hibernate session Session hibernateSession = hibernateSupport.getHibernateSession(); Preconditions.checkState(!hibernateSession.isOpen(), "Session should be closed after rollback0(false)"); if (log.isDebugEnabled()) { log.debug(this + " closed"); } }
protected void startTransaction() throws TopiaException { SessionFactory factory = hibernateSupport.getHibernateFactory(); Session result = factory.openSession(); hibernateSupport.setHibernateSession(result);
protected void rollback0(boolean beginAfterRollback) { try { Session hibernateSession = hibernateSupport.getHibernateSession(); Transaction transaction = hibernateSession.getTransaction(); hibernateSession.clear(); transaction.rollback(); hibernateSession.close(); sessionRegistry.unregister(hibernateSession); if (beginAfterRollback) { // it's very important to change the session after rollback // otherwise there are many error during next Entity's modification hibernateSession = hibernateSupport.getHibernateFactory().openSession(); hibernateSupport.setHibernateSession(hibernateSession); hibernateSession.setFlushMode(FlushMode.MANUAL); sessionRegistry.register(hibernateSession, this); hibernateSession.beginTransaction(); } getTopiaFiresSupport().fireOnPostRollback(this); } catch (HibernateException eee) { String message = String.format("An error occurred during rollback operation: %1$s", eee.getMessage()); throw new TopiaException(message, eee); } }
/** * Creating a new TopiaPersistenceContext is equivalent to creating a new transaction * * @param parameter everything needed (specific parameter-object) */ public AbstractTopiaPersistenceContext(AbstractTopiaPersistenceContextConstructorParameter parameter) { this.topiaIdFactory = parameter.getTopiaIdFactory(); this.sessionRegistry = parameter.getSessionRegistry(); this.firesSupport = new TopiaFiresSupport(parameter.getApplicationFiresSupport()); // Hibernate support can be created using the given hibernateProvider this.hibernateSupport = new InternalTopiaHibernateSupport(parameter.getHibernateProvider()); // Now starts the transaction, as this persistenceContext IS the TopiaTransaction startTransaction(); // Create the different supports that may be needed by the DAOs this.jpaSupport = new HibernateTopiaJpaSupport(hibernateSupport, firesSupport); this.sqlSupport = new HibernateTopiaSqlSupport(hibernateSupport); }
@Override public void commit() { checkNotClosed(); try { Session hibernateSession = hibernateSupport.getHibernateSession(); Transaction transaction = hibernateSession.getTransaction(); hibernateSession.flush(); transaction.commit(); getTopiaFiresSupport().fireOnPostCommit(this); hibernateSession.beginTransaction(); } catch (Exception eee) { String message = String.format("An error occurred during commit operation: %1$s", eee.getMessage()); throw new TopiaException(message, eee); } }