@Override public Session getSession(FlushMode flushMode) { Session session = sessionFactory.getCurrentSession(); session.setFlushMode(flushMode); return session; }
/** * Open a Session for the SessionFactory that this filter uses. * <p>The default implementation delegates to the {@link SessionFactory#openSession} * method and sets the {@link Session}'s flush mode to "MANUAL". * @param sessionFactory the SessionFactory that this filter uses * @return the Session to use * @throws DataAccessResourceFailureException if the Session could not be created * @see FlushMode#MANUAL */ @SuppressWarnings("deprecation") protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { try { Session session = sessionFactory.openSession(); session.setFlushMode(FlushMode.MANUAL); return session; } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } }
@SuppressWarnings("deprecation") @Nullable protected FlushMode prepareFlushMode(Session session, boolean readOnly) throws PersistenceException { FlushMode flushMode = (FlushMode) ReflectionUtils.invokeMethod(getFlushMode, session); Assert.state(flushMode != null, "No FlushMode from Session"); if (readOnly) { // We should suppress flushing for a read-only transaction. if (!flushMode.equals(FlushMode.MANUAL)) { session.setFlushMode(FlushMode.MANUAL); return flushMode; } } else { // We need AUTO or COMMIT for a non-read-only transaction. if (flushMode.lessThan(FlushMode.COMMIT)) { session.setFlushMode(FlushMode.AUTO); return flushMode; } } // No FlushMode change needed... return null; }
@Override @SuppressWarnings("deprecation") protected Session buildOrObtainSession() { Session session = super.buildOrObtainSession(); if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setFlushMode(FlushMode.MANUAL); } return session; }
/** * Open a Session for the SessionFactory that this interceptor uses. * <p>The default implementation delegates to the {@link SessionFactory#openSession} * method and sets the {@link Session}'s flush mode to "MANUAL". * @return the Session to use * @throws DataAccessResourceFailureException if the Session could not be created * @see FlushMode#MANUAL */ @SuppressWarnings("deprecation") protected Session openSession() throws DataAccessResourceFailureException { try { Session session = obtainSessionFactory().openSession(); session.setFlushMode(FlushMode.MANUAL); return session; } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } }
@Override public void enableAutoFlushMode() { em.unwrap(Session.class).setFlushMode(FlushMode.AUTO); }
@SuppressWarnings("deprecation") public void resetSessionState() { if (this.previousFlushMode != null) { this.session.setFlushMode(this.previousFlushMode); } if (this.preparedCon != null && this.session.isConnected()) { Connection conToReset = HibernateConnectionHandle.doGetConnection(this.session); if (conToReset != this.preparedCon) { LogFactory.getLog(HibernateJpaDialect.class).warn( "JDBC Connection to reset not identical to originally prepared Connection - please " + "make sure to use connection release mode ON_CLOSE (the default) and to run against " + "Hibernate 4.2+ (or switch HibernateJpaDialect's prepareConnection flag to false"); } DataSourceUtils.resetConnectionAfterTransaction(conToReset, this.previousIsolationLevel); } } }
/** * Open a Session for the given SessionFactory. * <p>The default implementation delegates to the {@link SessionFactory#openSession} * method and sets the {@link Session}'s flush mode to "MANUAL". * @param sessionFactory the SessionFactory to use * @return the Session to use * @throws DataAccessResourceFailureException if the Session could not be created * @since 5.0 * @see FlushMode#MANUAL */ @SuppressWarnings("deprecation") protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { Session session = openSession(); if (session == null) { try { session = sessionFactory.openSession(); session.setFlushMode(FlushMode.MANUAL); } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } } return session; }
@Override public void clearAutoFlushMode() { em.unwrap(Session.class).setFlushMode(FlushMode.MANUAL); }
@Override @SuppressWarnings("deprecation") protected Session buildOrObtainSession() { Session session = super.buildOrObtainSession(); if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setFlushMode(FlushMode.MANUAL); } return session; }
/** * Open a Session for the SessionFactory that this interceptor uses. * <p>The default implementation delegates to the {@link SessionFactory#openSession} * method and sets the {@link Session}'s flush mode to "MANUAL". * @return the Session to use * @throws DataAccessResourceFailureException if the Session could not be created * @see FlushMode#MANUAL */ @SuppressWarnings("deprecation") protected Session openSession() throws DataAccessResourceFailureException { try { Session session = obtainSessionFactory().openSession(); session.setFlushMode(FlushMode.MANUAL); return session; } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } }
@SuppressWarnings("deprecation") public void resetSessionState() { if (this.previousFlushMode != null) { this.session.setFlushMode(this.previousFlushMode); } if (this.preparedCon != null && this.session.isConnected()) { Connection conToReset = HibernateConnectionHandle.doGetConnection(this.session); if (conToReset != this.preparedCon) { LogFactory.getLog(HibernateJpaDialect.class).warn( "JDBC Connection to reset not identical to originally prepared Connection - please " + "make sure to use connection release mode ON_CLOSE (the default) and to run against " + "Hibernate 4.2+ (or switch HibernateJpaDialect's prepareConnection flag to false"); } DataSourceUtils.resetConnectionAfterTransaction(conToReset, this.previousIsolationLevel); } } }
@Override @SuppressWarnings("deprecation") public void beforeCompletion() { try { Session session = this.sessionHolder.getSession(); if (this.sessionHolder.getPreviousFlushMode() != null) { // In case of pre-bound Session, restore previous flush mode. session.setFlushMode(this.sessionHolder.getPreviousFlushMode()); } // Eagerly disconnect the Session here, to make release mode "on_close" work nicely. session.disconnect(); } finally { // Unbind at this point if it's a new Session... if (this.newSession) { TransactionSynchronizationManager.unbindResource(this.sessionFactory); this.holderActive = false; } } }
session.setFlushMode(FlushMode.MANUAL); isNew = true;
@Test public void testWierdSession() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Serializable id = s.save( new Foo() ); t.commit(); s.close(); s = openSession(); s.setFlushMode(FlushMode.MANUAL); t = s.beginTransaction(); Foo foo = (Foo) s.get(Foo.class, id); t.commit(); t = s.beginTransaction(); s.flush(); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); foo = (Foo) s.get(Foo.class, id); s.delete(foo); t.commit(); s.close(); }
session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
@Override @SuppressWarnings("deprecation") public void beforeCompletion() { try { Session session = this.sessionHolder.getSession(); if (this.sessionHolder.getPreviousFlushMode() != null) { // In case of pre-bound Session, restore previous flush mode. session.setFlushMode(this.sessionHolder.getPreviousFlushMode()); } // Eagerly disconnect the Session here, to make release mode "on_close" work nicely. session.disconnect(); } finally { // Unbind at this point if it's a new Session... if (this.newSession) { TransactionSynchronizationManager.unbindResource(this.sessionFactory); this.holderActive = false; } } }
@Before public void initFlush() throws IOException { Session session = getSession( getEntityManager() ); session.setFlushMode( getFlushMode() ); } }
@Test public void testFlushAutoSQLNativeSession() { doInJPA( this::entityManagerFactory, entityManager -> { entityManager.createNativeQuery( "delete from Person" ).executeUpdate();; } ); doInJPA( this::entityManagerFactory, entityManager -> { log.info( "testFlushAutoSQLNativeSession" ); //tag::flushing-auto-flush-sql-native-example[] assertTrue(((Number) entityManager .createNativeQuery( "select count(*) from Person") .getSingleResult()).intValue() == 0 ); Person person = new Person( "John Doe" ); entityManager.persist( person ); Session session = entityManager.unwrap(Session.class); // For this to work, the Session/EntityManager must be put into COMMIT FlushMode // - this is a change since 5.2 to account for merging EntityManager functionality // directly into Session. Flushing would be the JPA-spec compliant behavior, // so we know do that by default. session.setFlushMode( FlushModeType.COMMIT ); // or using Hibernate's FlushMode enum //session.setHibernateFlushMode( FlushMode.COMMIT ); assertTrue(((Number) session .createNativeQuery( "select count(*) from Person") .uniqueResult()).intValue() == 0 ); //end::flushing-auto-flush-sql-native-example[] } ); }
if (flushMode.equals(FlushMode.MANUAL) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setFlushMode(FlushMode.AUTO); sessionHolder.setPreviousFlushMode(flushMode); Session session = this.sessionFactory.openSession(); if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setFlushMode(FlushMode.MANUAL);