List of usage examples for org.springframework.orm.hibernate5 SessionHolder setSynchronizedWithTransaction
public void setSynchronizedWithTransaction(boolean synchronizedWithTransaction)
From source file:org.grails.orm.hibernate.GrailsSessionContext.java
protected void registerJtaSynchronization(Session session, SessionHolder sessionHolder) { // JTA synchronization is only possible with a javax.transaction.TransactionManager. // We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified // in Hibernate configuration, it will contain a TransactionManager reference. TransactionManager jtaTm = getJtaTransactionManager(session); if (jtaTm == null) { return;// www. j a v a2s. c o m } try { Transaction jtaTx = jtaTm.getTransaction(); if (jtaTx == null) { return; } int jtaStatus = jtaTx.getStatus(); if (jtaStatus != Status.STATUS_ACTIVE && jtaStatus != Status.STATUS_MARKED_ROLLBACK) { return; } LOG.debug("Registering JTA transaction synchronization for new Hibernate Session"); SessionHolder holderToUse = sessionHolder; // Register JTA Transaction with existing SessionHolder. // Create a new SessionHolder if none existed before. if (holderToUse == null) { holderToUse = new SessionHolder(session); } else { // it's up to the caller to manage concurrent sessions // holderToUse.addSession(session); } jtaTx.registerSynchronization( new SpringJtaSynchronizationAdapter(createSpringSessionSynchronization(holderToUse), jtaTm)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sessionHolder) { TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse); } } catch (Throwable ex) { throw new DataAccessResourceFailureException( "Could not register synchronization with JTA TransactionManager", ex); } }
From source file:org.grails.orm.hibernate.GrailsSessionContext.java
private Session createSession(Object resource) { LOG.debug("Opening Hibernate Session"); SessionHolder sessionHolder = (SessionHolder) resource; Session session = sessionFactory.openSession(); // Use same Session for further Hibernate actions within the transaction. // Thread object will get removed by synchronization at transaction completion. if (TransactionSynchronizationManager.isSynchronizationActive()) { // We're within a Spring-managed transaction, possibly from JtaTransactionManager. LOG.debug("Registering Spring transaction synchronization for new Hibernate Session"); SessionHolder holderToUse = sessionHolder; if (holderToUse == null) { holderToUse = new SessionHolder(session); } else {//w w w . j ava2 s . co m // it's up to the caller to manage concurrent sessions // holderToUse.addSession(session); } if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setFlushMode(FlushMode.MANUAL); } TransactionSynchronizationManager .registerSynchronization(createSpringSessionSynchronization(holderToUse)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sessionHolder) { TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse); } } else { // No Spring transaction management active -> try JTA transaction synchronization. registerJtaSynchronization(session, sessionHolder); } /* // Check whether we are allowed to return the Session. if (!allowCreate && !isSessionTransactional(session, sessionFactory)) { closeSession(session); throw new IllegalStateException("No Hibernate Session bound to thread, " + "and configuration does not allow creation of non-transactional one here"); } */ return session; }
From source file:org.springframework.orm.hibernate5.SpringSessionContext.java
/** * Retrieve the Spring-managed Session for the current thread, if any. *///from www .j a v a2s.c o m @Override @SuppressWarnings("deprecation") public Session currentSession() throws HibernateException { Object value = TransactionSynchronizationManager.getResource(this.sessionFactory); if (value instanceof Session) { return (Session) value; } else if (value instanceof SessionHolder) { SessionHolder sessionHolder = (SessionHolder) value; Session session = sessionHolder.getSession(); if (!sessionHolder.isSynchronizedWithTransaction() && TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.registerSynchronization( new SpringSessionSynchronization(sessionHolder, this.sessionFactory, false)); sessionHolder.setSynchronizedWithTransaction(true); // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session // with FlushMode.MANUAL, which needs to allow flushing within the transaction. FlushMode flushMode = SessionFactoryUtils.getFlushMode(session); if (flushMode.equals(FlushMode.MANUAL) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setFlushMode(FlushMode.AUTO); sessionHolder.setPreviousFlushMode(flushMode); } } return session; } if (this.transactionManager != null && this.jtaSessionContext != null) { try { if (this.transactionManager.getStatus() == Status.STATUS_ACTIVE) { Session session = this.jtaSessionContext.currentSession(); if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager .registerSynchronization(new SpringFlushSynchronization(session)); } return session; } } catch (SystemException ex) { throw new HibernateException("JTA TransactionManager found but status check failed", ex); } } if (TransactionSynchronizationManager.isSynchronizationActive()) { Session session = this.sessionFactory.openSession(); if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setFlushMode(FlushMode.MANUAL); } SessionHolder sessionHolder = new SessionHolder(session); TransactionSynchronizationManager.registerSynchronization( new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true)); TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder); sessionHolder.setSynchronizedWithTransaction(true); return session; } else { throw new HibernateException("Could not obtain transaction-synchronized Session for current thread"); } }