Java Code Examples for org.hibernate.context.internal.ManagedSessionContext#unbind()

The following examples show how to use org.hibernate.context.internal.ManagedSessionContext#unbind() . 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: DbClearRule.java    From flux with Apache License 2.0 6 votes vote down vote up
/**
 * Clears all given tables which are mentioned using the given sessionFactory
 */
private void clearDb(Class[] tables, SessionFactory sessionFactory) {
    Session session = sessionFactory.openSession();
    ManagedSessionContext.bind(session);
    Transaction tx = session.beginTransaction();
    try {
            session.createSQLQuery("set foreign_key_checks=0").executeUpdate();
        for (Class anEntity : tables) {
            session.createSQLQuery("delete from " + anEntity.getSimpleName() + "s").executeUpdate(); //table name is plural form of class name, so appending 's'
        }
            session.createSQLQuery("set foreign_key_checks=1").executeUpdate();
        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        throw new RuntimeException("Unable to clear tables. Exception: " + e.getMessage(), e);
    } finally {
        if (session != null) {
            ManagedSessionContext.unbind(sessionFactory);
            session.close();
        }
    }
}
 
Example 2
Source File: EventSchedulerDaoTest.java    From flux with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    Session session = sessionFactory.getSchedulerSessionFactory().openSession();
    ManagedSessionContext.bind(session);
    Transaction tx = session.beginTransaction();
    try {
        session.createSQLQuery("delete from ScheduledEvents").executeUpdate();
        tx.commit();
    } finally {
        if (session != null) {
            ManagedSessionContext.unbind(session.getSessionFactory());
            session.close();
            sessionFactory.clear();
        }
    }
}
 
Example 3
Source File: MessageDaoTest.java    From flux with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    context.setThreadLocalSession(context.getSchedulerSessionFactory().openSession());
    Session session = context.getThreadLocalSession();
    ManagedSessionContext.bind(session);
    Transaction tx = session.beginTransaction();
    try {
        session.createSQLQuery("delete from ScheduledMessages").executeUpdate();
        tx.commit();
    } finally {
        if(session != null) {
            ManagedSessionContext.unbind(context.getThreadLocalSession().getSessionFactory());
            session.close();
            context.clear();
        }
    }
}
 
Example 4
Source File: UnitOfWork.java    From commafeed with Apache License 2.0 6 votes vote down vote up
public static <T> T call(SessionFactory sessionFactory, SessionRunnerReturningValue<T> sessionRunner) {
	final Session session = sessionFactory.openSession();
	if (ManagedSessionContext.hasBind(sessionFactory)) {
		throw new IllegalStateException("Already in a unit of work!");
	}
	T t = null;
	try {
		ManagedSessionContext.bind(session);
		session.beginTransaction();
		try {
			t = sessionRunner.runInSession();
			commitTransaction(session);
		} catch (Exception e) {
			rollbackTransaction(session);
			UnitOfWork.<RuntimeException> rethrow(e);
		}
	} finally {
		session.close();
		ManagedSessionContext.unbind(sessionFactory);
	}
	return t;
}
 
Example 5
Source File: HibernateSessionExtension.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void afterEach(ExtensionContext context) {
    SessionContext sessionContext = getStore(context).get(context.getUniqueId(), SessionContext.class);
    if (sessionContext != null) {
        sessionContext.getSession().close();
        ManagedSessionContext.unbind(sessionContext.getSessionFactory());
        sessionContext.getSessionFactory().close();
    }
}
 
Example 6
Source File: TransactionAwareSessionContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
    * Creates a transaction synchronization object for the specified session.
    *
    * @param session
    *            Session to synchronize using the created object. Cannot be null.
    * @return A valid transaction synchronization. Never returns null.
    */
   private TransactionSynchronization createTransactionSynchronization(final Session session) {
return new TransactionSynchronizationAdapter() {
    @Override
    public void afterCompletion(final int status) {
	session.close();
	ManagedSessionContext.unbind(sessionFactory);
    }
};
   }
 
Example 7
Source File: ClientElbPersistenceServiceTest.java    From flux with Apache License 2.0 5 votes vote down vote up
private void clean() throws Exception {
    Session session = sessionFactory.getSchedulerSessionFactory().openSession();
    ManagedSessionContext.bind(session);
    Transaction tx = session.beginTransaction();
    try {
        session.createSQLQuery("delete from ClientElb").executeUpdate();
        tx.commit();
    } finally {
        if (session != null) {
            ManagedSessionContext.unbind(session.getSessionFactory());
            session.close();
            sessionFactory.clear();
        }
    }
}
 
Example 8
Source File: ClientElbDAOTest.java    From flux with Apache License 2.0 5 votes vote down vote up
private void clean() throws Exception {
    Session session = sessionFactory.getSchedulerSessionFactory().openSession();
    ManagedSessionContext.bind(session);
    Transaction tx = session.beginTransaction();
    try {
        session.createSQLQuery("delete from ClientElb").executeUpdate();
        tx.commit();
    } finally {
        if (session != null) {
            ManagedSessionContext.unbind(session.getSessionFactory());
            session.close();
            sessionFactory.clear();
        }
    }
}
 
Example 9
Source File: JobPersister.java    From robe with GNU Lesser General Public License v3.0 5 votes vote down vote up
public JobPersister(ConcurrentHashMap<String, JobInfo> jobs) {
    SessionFactory sessionFactory = GuiceBundle.getInjector().getInstance(SessionFactory.class);
    ManagedSessionContext.bind(sessionFactory.openSession());
    JobDao jobDao = new JobDao(sessionFactory);
    TriggerDao triggerDao = new TriggerDao(sessionFactory);
    for (JobInfo info : jobs.values()) {
        insertOrUpdate(jobDao, info, triggerDao);
    }
    sessionFactory.getCurrentSession().flush();
    sessionFactory.getCurrentSession().close();
    ManagedSessionContext.unbind(sessionFactory);
}
 
Example 10
Source File: RoleResourceTest.java    From robe with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void getRolePermissions() throws IOException {
    SessionFactory sessionFactory = RobeHibernateBundle.getInstance().getSessionFactory();
    ManagedSessionContext.bind(sessionFactory.openSession());
    RoleDao roleDao = new RoleDao(sessionFactory);
    Role role = roleDao.findByCode("all");
    Assert.assertTrue(role != null);
    TestRequest request = getRequestBuilder().endpoint(role.getId() + "/permissions").build();
    TestResponse response = client.get(request);
    Map result = response.get(Map.class);

    Assert.assertTrue(result.get("menu") != null);
    Assert.assertTrue(result.get("service") != null);
    ManagedSessionContext.unbind(sessionFactory);
}
 
Example 11
Source File: Transaction.java    From robe with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * If present, backup current the session in {@link ManagedSessionContext} and unbinds it.
 */
private void storePreviousSession() {
    if (ManagedSessionContext.hasBind(SESSION_FACTORY)) {
        SESSIONS.get().add(SESSION_FACTORY.getCurrentSession());
        ManagedSessionContext.unbind(SESSION_FACTORY);
    }
}
 
Example 12
Source File: Transaction.java    From robe with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Closes and unbinds the current session. <br/>
 * If {@link ManagedSessionContext} had a session before the current session, re-binds it to {@link ManagedSessionContext}
 */
private void finish() {
    try {
        if (session != null) {
            session.close();
        }
    } finally {
        ManagedSessionContext.unbind(SESSION_FACTORY);
        if (!SESSIONS.get().isEmpty()) {
            ManagedSessionContext.bind(SESSIONS.get().pop());
        }
    }
}
 
Example 13
Source File: UnitOfWorkInvoker.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Exchange exchange, Object o) {

    Object result;
    String methodname = this.getTargetMethod(exchange).getName();

    if (unitOfWorkMethods.containsKey(methodname)) {

        final Session session = sessionFactory.openSession();
        UnitOfWork unitOfWork = unitOfWorkMethods.get(methodname);

        try {
            configureSession(session, unitOfWork);
            ManagedSessionContext.bind(session);
            beginTransaction(session, unitOfWork);
            try {
                result = underlying.invoke(exchange, o);
                commitTransaction(session, unitOfWork);
                return result;
            } catch (Exception e) {
                rollbackTransaction(session, unitOfWork);
                this.<RuntimeException>rethrow(e); // unchecked rethrow
                return null; // avoid compiler warning
            }
        } finally {
            session.close();
            ManagedSessionContext.unbind(sessionFactory);
        }
    }
    else {
        return underlying.invoke(exchange, o);
    }
}