@Override public void flush() { SessionFactoryUtils.flush(getCurrentSession(), false); }
/** * Create a new HibernateTransactionManager instance. * @param sessionFactory the SessionFactory to manage transactions for */ public HibernateTransactionManager(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; afterPropertiesSet(); }
public void setSession(Session session) { this.sessionHolder = new SessionHolder(session); this.newSessionHolder = true; this.newSession = true; }
@Override protected Object doSuspend(Object transaction) { HibernateTransactionObject txObject = (HibernateTransactionObject) transaction; txObject.setSessionHolder(null); SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(obtainSessionFactory()); txObject.setConnectionHolder(null); ConnectionHolder connectionHolder = null; if (getDataSource() != null) { connectionHolder = (ConnectionHolder) TransactionSynchronizationManager.unbindResource(getDataSource()); } return new SuspendedResourcesHolder(sessionHolder, connectionHolder); }
public void setRollbackOnly() { getSessionHolder().setRollbackOnly(); if (hasConnectionHolder()) { getConnectionHolder().setRollbackOnly(); } }
@Override public boolean isRollbackOnly() { return getSessionHolder().isRollbackOnly() || (hasConnectionHolder() && getConnectionHolder().isRollbackOnly()); }
private void closeSession() { if (this.timeoutInProgress || this.errorInProgress) { logger.debug("Closing Hibernate Session after async request timeout/error"); SessionFactoryUtils.closeSession(this.sessionHolder.getSession()); } }
@Override public void beforeCommit(boolean readOnly) throws DataAccessException { if (!readOnly) { Session session = getCurrentSession(); // Read-write transaction -> flush the Hibernate Session. // Further check: only flush when not FlushMode.MANUAL. if (!FlushMode.MANUAL.equals(SessionFactoryUtils.getFlushMode(session))) { SessionFactoryUtils.flush(getCurrentSession(), true); } } }
@Override protected boolean isExistingTransaction(Object transaction) { HibernateTransactionObject txObject = (HibernateTransactionObject) transaction; return (txObject.hasSpringManagedTransaction() || (this.hibernateManagedSession && txObject.hasHibernateManagedTransaction())); }
/** * Create a new HibernateTemplate instance. * @param sessionFactory the SessionFactory to create Sessions with */ public HibernateTemplate(SessionFactory sessionFactory) { setSessionFactory(sessionFactory); afterPropertiesSet(); }
/** * Create a HibernateTemplate for the given SessionFactory. * Only invoked if populating the DAO with a SessionFactory reference! * <p>Can be overridden in subclasses to provide a HibernateTemplate instance * with different configuration, or a custom HibernateTemplate subclass. * @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for * @return the new HibernateTemplate instance * @see #setSessionFactory */ protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) { return new HibernateTemplate(sessionFactory); }
@Override public void update(Object entity) throws DataAccessException { update(entity, null); }
@Override public <T> T load(Class<T> entityClass, Serializable id) throws DataAccessException { return load(entityClass, id, null); }
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * <p>Will automatically apply a specified SQLExceptionTranslator to a * Hibernate JDBCException, else rely on Hibernate's default translation. * @param ex the HibernateException that occurred * @return a corresponding DataAccessException * @see SessionFactoryUtils#convertHibernateAccessException */ protected DataAccessException convertHibernateAccessException(HibernateException ex) { return SessionFactoryUtils.convertHibernateAccessException(ex); }
/** * Add the given annotated classes in a batch. * @see #addAnnotatedClass * @see #scanPackages */ public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClasses) { for (Class<?> annotatedClass : annotatedClasses) { addAnnotatedClass(annotatedClass); } return this; }
/** * Add the given annotated packages in a batch. * @see #addPackage * @see #scanPackages */ public LocalSessionFactoryBuilder addPackages(String... annotatedPackages) { for (String annotatedPackage : annotatedPackages) { addPackage(annotatedPackage); } return this; }
@Override public <T> List<T> findByExample(String entityName, T exampleEntity) throws DataAccessException { return findByExample(entityName, exampleEntity, -1, -1); }
@Override public Object getResourceFactory() { return obtainSessionFactory(); }
public void setExistingSession(Session session) { this.sessionHolder = new SessionHolder(session); this.newSessionHolder = true; this.newSession = false; }
@Override public <T> List<T> findByExample(T exampleEntity, int firstResult, int maxResults) throws DataAccessException { return findByExample(null, exampleEntity, firstResult, maxResults); }