Announcement Announcement Module
Collapse
No announcement yet.
Confusion with making Hibernate 3.2 work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    There are no links, just common sense.

    1) You are defining a TransactionManager in your application context, then let that also manage your transactions (don't do a session.startTransaction());

    2) You are using spring (TransactionManager and configured SessionFactory) then let spring manage your session. I.e. don't do openSession yourself because that would open a session which spring isn't aware off.

    Either use spring or don't use it but don't work against it (what is what you are doing when you configure the transactions/session factory in spring and then start/stop transactions and open sessions yourself).

    Comment


    • #17
      Been debugging with my own SessionFactoryUtils class and it looks like spring is being told its not allowed to create and new sessions.

      This is from the doGetSession method that is eventually triggered with a call to sf.getCurrentSession():

      Code:
      	private static Session doGetSession(
      			SessionFactory sessionFactory, Interceptor entityInterceptor,
      			SQLExceptionTranslator jdbcExceptionTranslator, boolean allowCreate)
      			throws HibernateException, IllegalStateException {
      //skip some stuffs...
      		logger.debug("Opening Hibernate Session TEST TEST TEST");
      		Session session = (entityInterceptor != null ?
      				sessionFactory.openSession(entityInterceptor) : 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.
      			logger.debug("Registering Spring transaction synchronization for new Hibernate Session");
      			SessionHolder holderToUse = sessionHolder;
      			if (holderToUse == null) {
      				holderToUse = new SessionHolder(session);
      			}
      			else {
      				holderToUse.addSession(session);
      			}
      			if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
      				session.setFlushMode(FlushMode.NEVER);
      			}
      			TransactionSynchronizationManager.registerSynchronization(
      					new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true));
      			holderToUse.setSynchronizedWithTransaction(true);
      			if (holderToUse != sessionHolder) {
      				TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);
      			}
      		}
      		else {
      			// No Spring transaction management active -> try JTA transaction synchronization.
      			registerJtaSynchronization(session, sessionFactory, jdbcExceptionTranslator, sessionHolder);
      		}
      
      		System.out.println("allowCreate: " + allowCreate);
      		System.out.println("isSessionTransactional: " + isSessionTransactional(session, sessionFactory));
      
      		// 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;
      	}
      debug has my TEST TEST TEST stuff in it, and the system out shows that allowCreate: false
      isSessionTransactional: false
      Which is closing the session and blowing up. Now I can understand not being inside a transaction yet since this is the first call and everythings being initialized still. But why is allowCreate false? Is that a property that I can turn on in the session factory?

      Comment


      • #18
        You shouldn't need allowCreate, also if you would need it you would get a non-transactional one. With proper transaction configuration and transactions you will get a Session (because the first if TransactionSynchronizationManager.isSynchronizatio nActive() will return true).

        So you have some issue with your transactions.

        Comment


        • #19
          Fixed

          Turns out it was transactions not working due to incorrectly loading the context.xml file. See this post about the transaction issue:
          http://forum.springframework.org/showthread.php?t=52759

          Comment

          Working...
          X