Java Code Examples for org.hibernate.Session#getTransaction()
The following examples show how to use
org.hibernate.Session#getTransaction() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheHibernateStoreSessionListener.java From ignite with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override 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(); } } }
Example 2
Source File: HibernateTransactionHelper.java From webdsl with Apache License 2.0 | 6 votes |
public static void rollbackAndStartNewTransaction() { Session hibSession = HibernateUtil.getCurrentSession(); Transaction existingTransaction = hibSession.getTransaction(); try { existingTransaction.rollback(); } catch (Exception ex) { org.webdsl.logging.Logger.error(ex); ex.printStackTrace(); existingTransaction.rollback(); } finally { Session newSession = ThreadLocalPage.get() .openNewTransactionThroughGetCurrentSession(); newSession.setFlushMode(FlushMode.COMMIT); ThreadLocalPage.get().clearEntitiesToBeValidated(); ThreadLocalPage.get().initVarsAndArgs(); } }
Example 3
Source File: CacheHibernateBlobStoreSelfTest.java From ignite with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { super.afterTest(); Session s = store.session(null); if (s == null) return; try { s.createQuery("delete from " + CacheHibernateBlobStoreEntry.class.getSimpleName()) .setFlushMode(FlushMode.ALWAYS).executeUpdate(); Transaction hTx = s.getTransaction(); if (hTx != null && hTx.getStatus() == TransactionStatus.ACTIVE) hTx.commit(); } finally { s.close(); } }
Example 4
Source File: CacheHibernateBlobStoreSelfTest.java From ignite with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { super.afterTest(); Session s = store.session(null); if (s == null) return; try { s.createQuery("delete from " + CacheHibernateBlobStoreEntry.class.getSimpleName()) .setFlushMode(FlushMode.ALWAYS).executeUpdate(); Transaction hTx = s.getTransaction(); if (hTx != null && hTx.isActive()) hTx.commit(); } finally { s.close(); } }
Example 5
Source File: CacheHibernateBlobStoreSelfTest.java From ignite with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override protected void afterTest() throws Exception { super.afterTest(); Session s = store.session(null); if (s == null) return; try { s.createQuery("delete from " + CacheHibernateBlobStoreEntry.class.getSimpleName()) .setFlushMode(FlushMode.ALWAYS).executeUpdate(); Transaction hTx = s.getTransaction(); if (hTx != null && hTx.getStatus() == TransactionStatus.ACTIVE) hTx.commit(); } finally { s.close(); } }
Example 6
Source File: FooFixtures.java From tutorials with MIT License | 5 votes |
public void createBars() { Session session = null; Transaction tx = null; session = sessionFactory.openSession(); tx = session.getTransaction(); try { tx.begin(); for (int i = 156; i < 160; i++) { final Bar bar = new Bar(); bar.setName("Bar_" + i); final Foo foo = new Foo("Foo_" + (i + 120)); foo.setBar(bar); session.save(foo); final Foo foo2 = new Foo(null); if (i % 2 == 0) foo2.setName("LuckyFoo" + (i + 120)); foo2.setBar(bar); session.save(foo2); bar.getFooSet().add(foo); bar.getFooSet().add(foo2); session.merge(bar); } tx.commit(); session.flush(); } catch (final HibernateException he) { if (tx != null) tx.rollback(); System.out.println("Not able to open session"); he.printStackTrace(); } catch (final Exception e) { e.printStackTrace(); } finally { if (session != null) session.close(); } }
Example 7
Source File: _BaseRootDAO.java From unitime with Apache License 2.0 | 5 votes |
/** * Begin the transaction related to the session */ public Transaction beginTransaction(Session s) { // already in a transaction, do not create a new one if (s.getTransaction() != null && s.getTransaction().isActive()) return null; return s.beginTransaction(); }
Example 8
Source File: CacheHibernateBlobStore.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public void sessionEnd(boolean commit) { init(); Transaction tx = transaction(); Map<String, Session> props = session().properties(); Session ses = props.remove(ATTR_SES); if (ses != null) { org.hibernate.Transaction hTx = ses.getTransaction(); if (hTx != null) { try { if (commit) { ses.flush(); hTx.commit(); } else hTx.rollback(); if (log.isDebugEnabled()) log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']'); } catch (HibernateException e) { throw new CacheWriterException("Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e); } finally { ses.close(); } } } }
Example 9
Source File: CacheHibernateBlobStore.java From ignite with Apache License 2.0 | 5 votes |
/** * Ends hibernate session. * * @param ses Hibernate session. * @param tx Cache ongoing transaction. */ private void end(Session ses, Transaction tx) { // Commit only if there is no cache transaction, // otherwise sessionEnd() will do all required work. if (tx == null) { org.hibernate.Transaction hTx = ses.getTransaction(); if (hTx != null && hTx.isActive()) hTx.commit(); ses.close(); } }
Example 10
Source File: CacheHibernateBlobStore.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public void sessionEnd(boolean commit) { init(); Transaction tx = transaction(); Map<String, Session> props = session().properties(); Session ses = props.remove(ATTR_SES); if (ses != null) { org.hibernate.Transaction hTx = ses.getTransaction(); if (hTx != null) { try { if (commit) { ses.flush(); hTx.commit(); } else hTx.rollback(); if (log.isDebugEnabled()) log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']'); } catch (HibernateException e) { throw new CacheWriterException("Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e); } finally { ses.close(); } } } }
Example 11
Source File: CacheHibernateBlobStore.java From ignite with Apache License 2.0 | 5 votes |
/** * Ends hibernate session. * * @param ses Hibernate session. * @param tx Cache ongoing transaction. */ private void end(Session ses, Transaction tx) { // Commit only if there is no cache transaction, // otherwise sessionEnd() will do all required work. if (tx == null) { org.hibernate.Transaction hTx = ses.getTransaction(); if (hTx != null && hTx.getStatus() == TransactionStatus.ACTIVE) hTx.commit(); ses.close(); } }
Example 12
Source File: CacheHibernateBlobStore.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public void sessionEnd(boolean commit) { init(); Transaction tx = transaction(); Map<String, Session> props = session().properties(); Session ses = props.remove(ATTR_SES); if (ses != null) { org.hibernate.Transaction hTx = ses.getTransaction(); if (hTx != null) { try { if (commit) { ses.flush(); hTx.commit(); } else hTx.rollback(); if (log.isDebugEnabled()) log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']'); } catch (HibernateException e) { throw new CacheWriterException("Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e); } finally { ses.close(); } } } }
Example 13
Source File: CacheHibernateBlobStore.java From ignite with Apache License 2.0 | 5 votes |
/** * Ends hibernate session. * * @param ses Hibernate session. * @param tx Cache ongoing transaction. */ private void end(Session ses, Transaction tx) { // Commit only if there is no cache transaction, // otherwise sessionEnd() will do all required work. if (tx == null) { org.hibernate.Transaction hTx = ses.getTransaction(); if (hTx != null && hTx.getStatus() == TransactionStatus.ACTIVE) hTx.commit(); ses.close(); } }
Example 14
Source File: CacheHibernateStoreSessionListenerSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** */ private void checkSession() { Session hibSes = ses.attachment(); assertNotNull(hibSes); assertTrue(hibSes.isOpen()); Transaction tx = hibSes.getTransaction(); assertNotNull(tx); if (ses.isWithinTransaction()) assertEquals(TransactionStatus.ACTIVE, tx.getStatus()); else assertFalse("Unexpected status: " + tx.getStatus(), tx.getStatus() == TransactionStatus.ACTIVE); verifySameInstance(hibSes); }
Example 15
Source File: HibernateLifecycleUtil.java From tutorials with MIT License | 4 votes |
public static Transaction startTransaction(Session s) { Transaction tx = s.getTransaction(); tx.begin(); return tx; }
Example 16
Source File: UnitOfWork.java From commafeed with Apache License 2.0 | 4 votes |
private static void commitTransaction(Session session) { final Transaction txn = session.getTransaction(); if (txn != null && txn.isActive()) { txn.commit(); } }
Example 17
Source File: CacheHibernateStoreSessionListenerSelfTest.java From ignite with Apache License 2.0 | 4 votes |
/** */ private void checkSession() { Session hibSes = ses.attachment(); assertNotNull(hibSes); assertTrue(hibSes.isOpen()); Transaction tx = hibSes.getTransaction(); assertNotNull(tx); if (ses.isWithinTransaction()) assertEquals(TransactionStatus.ACTIVE, tx.getStatus()); else assertFalse("Unexpected status: " + tx.getStatus(), tx.getStatus() == TransactionStatus.ACTIVE); verifySameInstance(hibSes); }
Example 18
Source File: UnitOfWork.java From commafeed with Apache License 2.0 | 4 votes |
private static void rollbackTransaction(Session session) { final Transaction txn = session.getTransaction(); if (txn != null && txn.isActive()) { txn.rollback(); } }
Example 19
Source File: Exam.java From unitime with Apache License 2.0 | 4 votes |
public String unassign(String managerExternalId, Session hibSession) { Transaction tx = null; try { if (hibSession.getTransaction()==null || !hibSession.getTransaction().isActive()) tx = hibSession.beginTransaction(); ExamAssignment oldAssignment = new ExamAssignment(this); setAssignedPeriod(null); if (getAssignedRooms()==null) setAssignedRooms(new HashSet()); getAssignedRooms().clear(); setAssignedPreference(null); HashSet otherExams = new HashSet(); for (Iterator j=getConflicts().iterator();j.hasNext();) { ExamConflict conf = (ExamConflict)j.next(); for (Iterator i=conf.getExams().iterator();i.hasNext();) { Exam x = (Exam)i.next(); if (!x.equals(this)) { x.getConflicts().remove(conf); otherExams.add(x); } } hibSession.delete(conf); j.remove(); } ExamEvent event = getEvent(); if (event!=null) hibSession.delete(event); hibSession.update(this); for (Iterator i=otherExams.iterator();i.hasNext();) hibSession.update((Exam)i.next()); SubjectArea subject = null; Department dept = null; for (Iterator i=new TreeSet(getOwners()).iterator();i.hasNext();) { ExamOwner owner = (ExamOwner)i.next(); subject = owner.getCourse().getSubjectArea(); dept = subject.getDepartment(); break; } ChangeLog.addChange(hibSession, TimetableManager.findByExternalId(managerExternalId), getSession(), this, getName()+" ("+ (oldAssignment.getPeriod()==null?"N/A":oldAssignment.getPeriodAbbreviation()+" "+oldAssignment.getRoomsName(", "))+ " → N/A)", ChangeLog.Source.EXAM_INFO, ChangeLog.Operation.UNASSIGN, subject, dept); if (tx!=null) tx.commit(); return null; } catch (Exception e) { if (tx!=null) tx.rollback(); e.printStackTrace(); return "Unassignment of "+getName()+" failed, reason: "+e.getMessage(); } }
Example 20
Source File: Application.java From coditori with Apache License 2.0 | 3 votes |
public static void main(String[] args) { Session s = HibernateConfig.getSession(); Transaction tx= s.getTransaction(); User user = new User(); user.setName("Masoud"); tx.begin(); s.save(user); tx.commit(); s.close(); }