public static void setUp() { AnnotationConfiguration config = new AnnotationConfiguration(); config.setProperty("hibernate.current_session_context_class", "managed"); config.setProperty("hibernate.c3p0.max_size", "20").setProperty( "hibernate.c3p0.timeout", "3000").setProperty( "hibernate.c3p0.idle_test_period", "300").setProperty("hibernate.hbm2ddl.auto", "update"); ConstellioAnnotationUtils.addAnnotatedClasses(config); sessionFactory = config.buildSessionFactory(); Application.set(new DataDummyWebApplication()); org.hibernate.classic.Session hibernateSession = sessionFactory.openSession(); hibernateSession.beginTransaction(); ManagedSessionContext.bind(hibernateSession); }
public static void tearDown() { try { if (ManagedSessionContext.hasBind(sessionFactory)) { EntityManager currentEntityManager = ConstellioPersistenceContext.getCurrentEntityManager(); if (currentEntityManager.isOpen()) { try { if (currentEntityManager.getTransaction().isActive()) currentEntityManager.getTransaction().rollback(); } finally { currentEntityManager.close(); } } ManagedSessionContext.unbind(sessionFactory); } } finally { if (sessionFactory != null) { sessionFactory.close(); } } }
public void endWork() { if (UnitOfWork.TRANSACTION.equals(unitOfWork)) throw new IllegalStateException("Cannot use WorkManager with UnitOfWork.TRANSACTION"); //do nothing if there is no session open SessionFactory sessionFactory = sessionFactoryProvider.get(); if (!ManagedSessionContext.hasBind(sessionFactory)) return; //close up session when done try { sessionFactory.getCurrentSession().close(); } finally { //discard session; ManagedSessionContext.unbind(sessionFactory); } }
private CurrentSessionContext buildCurrentSessionContext() { String impl = properties.getProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS ); // for backward-compatability if ( impl == null && transactionManager != null ) { impl = "jta"; } if ( impl == null ) { return null; } else if ( "jta".equals( impl ) ) { if ( settings.getTransactionFactory().areCallbacksLocalToHibernateTransactions() ) { log.warn( "JTASessionContext being used with JDBCTransactionFactory; auto-flush will not operate correctly with getCurrentSession()" ); } return new JTASessionContext( this ); } else if ( "thread".equals( impl ) ) { return new ThreadLocalSessionContext( this ); } else if ( "managed".equals( impl ) ) { return new ManagedSessionContext( this ); } else { try { Class implClass = ReflectHelper.classForName( impl ); return ( CurrentSessionContext ) implClass .getConstructor( new Class[] { SessionFactoryImplementor.class } ) .newInstance( new Object[] { this } ); } catch( Throwable t ) { log.error( "Unable to construct current session context [" + impl + "]", t ); return null; } } }
/** * Close the current session and unbind it via {@link ManagedSessionContext#unbind(SessionFactory)}. The hibernate * property "hibernate.current_session_context_class" must be set to "managed" for this to have effect. This method * should be called from within an Interceptor or Filter type class that is setting up the scope of the Session, * when this scope is about to expire. */ public void unbindAndCleanupSession() { org.hibernate.classic.Session currentSession = ManagedSessionContext.unbind(getSessionFactory()); if (currentSession != null) { currentSession.close(); } }
public void beginWork() { if (UnitOfWork.TRANSACTION.equals(unitOfWork)) throw new IllegalStateException("Cannot use WorkManager with UnitOfWork.TRANSACTION"); //do nothing if a session is already open if (ManagedSessionContext.hasBind(sessionFactoryProvider.get())) return; //open session; ManagedSessionContext.bind(sessionFactoryProvider.get().openSession()); }
@AfterClass void post() { final SessionFactory sessionFactory = injector.getInstance(SessionFactory.class); sessionFactory.close(); ManagedSessionContext.unbind(sessionFactory); }
/** * Open a hibernate session and bind it as the current session via * {@link ManagedSessionContext#bind(org.hibernate.classic.Session)}. The hibernate property * "hibernate.current_session_context_class" must be set to "managed" for this to have effect This method should be * called from within an Interceptor or Filter type class that is setting up the scope of the Session. This method * should then call {@link HibernateUtil#unbindAndCleanupSession()} when the scope of the Session is expired. * * @see ManagedSessionContext#bind(org.hibernate.classic.Session) */ public void openAndBindSession() { SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) getSessionFactory(); org.hibernate.classic.Session currentSession = sessionFactoryImplementor.openSession(null, true, false, ConnectionReleaseMode.AFTER_STATEMENT); currentSession.setFlushMode(FlushMode.COMMIT); ManagedSessionContext.bind(currentSession); }