List of usage examples for org.hibernate.resource.transaction.spi TransactionStatus MARKED_ROLLBACK
TransactionStatus MARKED_ROLLBACK
To view the source code for org.hibernate.resource.transaction.spi TransactionStatus MARKED_ROLLBACK.
Click Source Link
From source file:com.booleanworks.kryptopterus.application.MainHibernateUtil.java
License:Apache License
public Object saveOrUpdate(Object object, Session session) { System.out.println("com.booleanworks.kryptopterus.application.MainHibernateUtil.saveOrUpdate()"); System.out.println("session => " + session.hashCode()); Object result = null;/*from w w w . j a v a2 s . c o m*/ if (session == null || !session.isConnected() || !session.isOpen()) { session = this.getResidentSession(); } if (session.isJoinedToTransaction()) { session.saveOrUpdate(object); if (!(session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK)) { session.flush(); } result = session.get(object.getClass(), session.getIdentifier(object)); } else { Transaction transaction = this.beginTransaction(session, false); session.saveOrUpdate(object); if (!transaction.getRollbackOnly() && session.getFlushMode() != FlushModeType.AUTO) { session.flush(); } result = session.get(object.getClass(), session.getIdentifier(object)); this.commitTransaction(session, transaction); } return result; }
From source file:com.booleanworks.kryptopterus.application.MainHibernateUtil.java
License:Apache License
public Object delete(Object object, Session session) { System.out.println("com.booleanworks.kryptopterus.application.MainHibernateUtil.delete()"); System.out.println("session => " + session.hashCode()); Object result = null;/*w ww . ja va 2 s.c o m*/ if (session == null || !session.isConnected() || !session.isOpen()) { session = this.getResidentSession(); } if (session.isJoinedToTransaction()) { session.delete(object); if (!(session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK)) { session.flush(); } //session.detach(object) ; result = object; } else { Transaction transaction = this.beginTransaction(session, false); session.delete(object); session.flush(); //session.detach(object) ; result = object; this.commitTransaction(session, transaction); } return result; }
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 {// www. ja v a 2 s. 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"); } }