Exemplo n.º 1
0
 /** Retrieve the Spring-managed Session for the current thread, if any. */
 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 (TransactionSynchronizationManager.isSynchronizationActive()
         && !sessionHolder.isSynchronizedWithTransaction()) {
       TransactionSynchronizationManager.registerSynchronization(
           new SpringSessionSynchronization(sessionHolder, this.sessionFactory));
       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 = session.getFlushMode();
       if (FlushMode.isManualFlushMode(flushMode)
           && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
         session.setFlushMode(FlushMode.AUTO);
         sessionHolder.setPreviousFlushMode(flushMode);
       }
     }
     return session;
   } else if (this.jtaSessionContext != null) {
     Session session = this.jtaSessionContext.currentSession();
     if (TransactionSynchronizationManager.isSynchronizationActive()) {
       TransactionSynchronizationManager.registerSynchronization(
           new SpringFlushSynchronization(session));
     }
     return session;
   } else {
     throw new HibernateException("No Session found for current thread");
   }
 }
 private static FlushMode getFlushMode(String flushMode) {
   if (flushMode == null) {
     return null;
   }
   flushMode = flushMode.toUpperCase();
   return FlushMode.parse(flushMode);
 }
 private static FlushMode convertFlushMode(Object object) {
   if (object == null) {
     return null;
   }
   if (object instanceof FlushMode) {
     return (FlushMode) object;
   }
   return FlushMode.parse(String.valueOf(object));
 }