List of usage examples for org.hibernate Transaction getStatus
TransactionStatus getStatus();
From source file:com.quakearts.webapp.hibernate.CurrentSessionContextHelper.java
License:Open Source License
public static void closeOpenSessions() { ArrayList<Exception> exceptions = new ArrayList<>(); for (CurrentSessionContextHelper helper : sessionHelperCache.values()) { Session session = helper.getCurrentSessionFromContextAttributes(); if (session == null) return; helper.removeCurrentSessionFromContextAttributes(); Transaction tx = session.getTransaction(); if (tx == null) throw new IllegalStateException("Transaction cannot be found."); try {// w ww . j av a 2s . co m if (tx.getStatus() == TransactionStatus.ACTIVE) { tx.commit(); } else if (tx.getStatus() == TransactionStatus.MARKED_ROLLBACK) { tx.rollback(); } else { throw new IllegalStateException("Transaction is not in an epected state"); } } catch (HibernateException e) { exceptions.add(new Exception("Exception thrown whiles cleaning up domain: " + helper.domain, e)); } finally { try {//Reclose just in case session.close(); } catch (SessionException e) { //Ignore } catch (Exception e) { exceptions .add(new Exception("Exception thrown whiles cleaning up domain: " + helper.domain, e)); } } } if (exceptions.size() > 0) { for (Exception e : exceptions) { log.log(Level.SEVERE, e.getMessage(), e); } throw new IllegalStateException("Error during session cleanup"); } }
From source file:io.dropwizard.sharding.utils.TransactionHandler.java
License:Apache License
private void rollbackTransaction() { final Transaction txn = session.getTransaction(); if (txn != null && txn.getStatus() == TransactionStatus.ACTIVE) { txn.rollback();//from w w w. ja va2s . c om } }
From source file:io.dropwizard.sharding.utils.TransactionHandler.java
License:Apache License
private void commitTransaction() { final Transaction txn = session.getTransaction(); if (txn != null && txn.getStatus() == TransactionStatus.ACTIVE) { txn.commit();//from w ww . j a v a 2 s .co m } }
From source file:org.apache.ignite.cache.store.hibernate.CacheHibernateStoreSessionListener.java
License:Apache License
/** {@inheritDoc} */ @Override/*from w w w . j a v a 2 s . com*/ public void onSessionEnd(CacheStoreSession ses, boolean commit) { Session hibSes = ses.attach(null); if (hibSes != null) { try { Transaction tx = hibSes.getTransaction(); if (commit) { if (hibSes.isDirty()) hibSes.flush(); if (tx.getStatus() == TransactionStatus.ACTIVE) tx.commit(); } else if (tx.getStatus().canRollback()) tx.rollback(); } catch (HibernateException e) { throw new CacheWriterException("Failed to end store session [tx=" + ses.transaction() + ']', e); } finally { hibSes.close(); } } }
From source file:org.glite.security.voms.admin.integration.orgdb.tools.OrgDBUtil.java
License:Apache License
public OrgDBUtil(String[] args) throws ParseException, FileNotFoundException, IOException { setupCLParser();// www . ja v a 2s . c o m parseCommandLine(args); initializeDatabase(); execute(); Transaction tx = OrgDBSessionFactory.getSessionFactory().getCurrentSession().getTransaction(); TransactionStatus status = tx.getStatus(); if (tx.isActive() && !status.equals(TransactionStatus.COMMITTED) && !status.equals(TransactionStatus.COMMITTING)) { tx.commit(); } }
From source file:org.glite.security.voms.admin.persistence.HibernateFactory.java
License:Apache License
public static void commitTransaction() { Transaction tx = (Transaction) threadTransaction.get(); try {//from w w w .java 2s . c o m if (tx != null && !tx.getStatus().equals(TransactionStatus.COMMITTED) && !tx.getStatus().equals(TransactionStatus.ROLLING_BACK) && !tx.getStatus().equals(TransactionStatus.ROLLED_BACK)) { if (log.isDebugEnabled()) { log.debug("Committing transaction {} for thread {}", tx, Thread.currentThread()); } tx.commit(); } threadTransaction.set(null); } catch (HibernateException ex) { log.error("Error committing hibernate transaction:" + ex.getMessage()); rollbackTransaction(); if (log.isDebugEnabled()) { log.error("Error committing hibernate transaction!", ex); } throw new VOMSDatabaseException(ex.getMessage(), ex); } }
From source file:org.glite.security.voms.admin.persistence.HibernateFactory.java
License:Apache License
public static void rollbackTransaction() { Transaction tx = (Transaction) threadTransaction.get(); TransactionStatus status = tx.getStatus(); log.warn("Rolling back transaction {}", tx); try {//from w w w . j a v a 2s .c om threadTransaction.set(null); if (tx != null && !tx.getStatus().equals(TransactionStatus.COMMITTED) && !tx.getStatus().equals(TransactionStatus.ROLLING_BACK) && !tx.getStatus().equals(TransactionStatus.ROLLED_BACK)) { if (log.isDebugEnabled()) { log.debug("Rolling back transaction {} for thread {}", tx, Thread.currentThread()); } tx.rollback(); } } catch (HibernateException ex) { log.error("Error rolling back hibernate transaction:" + ex.getMessage(), ex); throw new VOMSDatabaseException(ex.getMessage(), ex); } finally { closeSession(); } }
From source file:org.infinispan.test.hibernate.cache.AbstractNonFunctionalTest.java
License:LGPL
protected <T> T withTx(NodeEnvironment environment, SessionImplementor session, Callable<T> callable) throws Exception { if (jtaPlatform != null) { TransactionManager tm = environment.getServiceRegistry().getService(JtaPlatform.class) .retrieveTransactionManager(); return Caches.withinTx(tm, callable); } else {/*from ww w . j a va2 s .c o m*/ Transaction transaction = ((Session) session).beginTransaction(); boolean rollingBack = false; try { T retval = callable.call(); if (transaction.getStatus() == TransactionStatus.ACTIVE) { transaction.commit(); } else { rollingBack = true; transaction.rollback(); } return retval; } catch (Exception e) { if (!rollingBack) { try { transaction.rollback(); } catch (Exception suppressed) { e.addSuppressed(suppressed); } } throw e; } } }
From source file:org.infinispan.test.hibernate.cache.commons.AbstractNonFunctionalTest.java
License:LGPL
protected <T> T withTx(NodeEnvironment environment, Object session, Callable<T> callable) throws Exception { TransactionManager tm = environment.getServiceRegistry().getService(JtaPlatform.class) .retrieveTransactionManager(); if (tm != null) { return Caches.withinTx(tm, callable); } else {/*from w w w. j a v a 2 s.c o m*/ Transaction transaction = TEST_SESSION_ACCESS.beginTransaction(session); boolean rollingBack = false; try { T retval = callable.call(); if (transaction.getStatus() == TransactionStatus.ACTIVE) { transaction.commit(); } else { rollingBack = true; transaction.rollback(); } return retval; } catch (Exception e) { if (!rollingBack) { try { transaction.rollback(); } catch (Exception suppressed) { e.addSuppressed(suppressed); } } throw e; } } }
From source file:org.jboss.additional.testsuite.jdkall.past.jpa.hibernate.secondlevelcache.SFSB.java
License:Open Source License
public Student createStudent(String firstName, String lastName, String address, int id) { // setupConfig(); Student student = new Student(); student.setStudentId(id);// w ww . j a v a 2 s .com student.setAddress(address); student.setFirstName(firstName); student.setLastName(lastName); try { Session session = sessionFactory.openSession(); Transaction ormTransaction = session.beginTransaction(); // join the current JTA transaction TransactionStatus status = ormTransaction.getStatus(); if (status.isNotOneOf(TransactionStatus.ACTIVE)) { throw new RuntimeException( "Hibernate Transaction is not active after joining Hibernate to JTA transaction: " + status.name()); } session.save(student); // trans.commit(); session.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Failure while persisting student entity", e); } return student; }