org.hibernate.Session#getTransaction ( )源码实例Demo

下面列出了org.hibernate.Session#getTransaction ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/** {@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();
        }
    }
}
 
源代码2 项目: ignite   文件: CacheHibernateBlobStoreSelfTest.java
/** {@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();
    }
}
 
源代码3 项目: ignite   文件: CacheHibernateBlobStoreSelfTest.java
/** {@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();
    }
}
 
源代码4 项目: ignite   文件: CacheHibernateBlobStoreSelfTest.java
/** {@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();
    }
}
 
源代码5 项目: webdsl   文件: HibernateTransactionHelper.java
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();
	}
}
 
源代码6 项目: ignite   文件: CacheHibernateBlobStore.java
/**
 * 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();
    }
}
 
源代码7 项目: ignite   文件: CacheHibernateBlobStore.java
/** {@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();
            }
        }
    }
}
 
源代码8 项目: ignite   文件: CacheHibernateBlobStore.java
/**
 * 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();
    }
}
 
源代码9 项目: ignite   文件: CacheHibernateBlobStore.java
/** {@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();
            }
        }
    }
}
 
源代码10 项目: ignite   文件: CacheHibernateBlobStore.java
/**
 * 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();
    }
}
 
源代码11 项目: ignite   文件: CacheHibernateBlobStore.java
/** {@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();
            }
        }
    }
}
 
源代码12 项目: unitime   文件: _BaseRootDAO.java
/**
 * 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();
}
 
源代码13 项目: tutorials   文件: FooFixtures.java
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();
    }

}
 
/**
 */
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);
}
 
源代码15 项目: commafeed   文件: UnitOfWork.java
private static void commitTransaction(Session session) {
	final Transaction txn = session.getTransaction();
	if (txn != null && txn.isActive()) {
		txn.commit();
	}
}
 
/**
 */
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);
}
 
源代码17 项目: tutorials   文件: HibernateLifecycleUtil.java
public static Transaction startTransaction(Session s) {
    Transaction tx = s.getTransaction();
    tx.begin();
    return tx;
}
 
源代码18 项目: commafeed   文件: UnitOfWork.java
private static void rollbackTransaction(Session session) {
	final Transaction txn = session.getTransaction();
	if (txn != null && txn.isActive()) {
		txn.rollback();
	}
}
 
源代码19 项目: unitime   文件: Exam.java
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(", "))+
                " &rarr; 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();
    }
}
 
源代码20 项目: coditori   文件: Application.java
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();
    }