Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Confusion with making Hibernate 3.2 work

  1. #11
    Join Date
    Apr 2008
    Posts
    21

    Default

    Also while using openSession my spring connection pool is filling. Was not paying attention to it until I turned on sessionfactory statistics. This is what last came out before my 22 connection pool filled up.

    Code:
    07-Apr-2008 05:45:01 PM: [INFO] Logging statistics....
    07-Apr-2008 05:45:01 PM: [INFO] start time: 1207604264740
    07-Apr-2008 05:45:01 PM: [INFO] sessions opened: 21
    07-Apr-2008 05:45:01 PM: [INFO] sessions closed: 0
    07-Apr-2008 05:45:01 PM: [INFO] transactions: 0
    07-Apr-2008 05:45:01 PM: [INFO] successful transactions: 0
    07-Apr-2008 05:45:01 PM: [INFO] optimistic lock failures: 0
    07-Apr-2008 05:45:01 PM: [INFO] flushes: 0
    07-Apr-2008 05:45:01 PM: [INFO] connections obtained: 22
    07-Apr-2008 05:45:01 PM: [INFO] statements prepared: 23
    07-Apr-2008 05:45:01 PM: [INFO] statements closed: 23
    07-Apr-2008 05:45:01 PM: [INFO] second level cache puts: 0
    07-Apr-2008 05:45:01 PM: [INFO] second level cache hits: 0
    07-Apr-2008 05:45:01 PM: [INFO] second level cache misses: 0
    07-Apr-2008 05:45:01 PM: [INFO] entities loaded: 3406
    07-Apr-2008 05:45:01 PM: [INFO] entities updated: 0
    07-Apr-2008 05:45:01 PM: [INFO] entities inserted: 0
    07-Apr-2008 05:45:01 PM: [INFO] entities deleted: 0
    07-Apr-2008 05:45:01 PM: [INFO] entities fetched (minimize this): 0
    07-Apr-2008 05:45:01 PM: [INFO] collections loaded: 1
    07-Apr-2008 05:45:01 PM: [INFO] collections updated: 0
    07-Apr-2008 05:45:01 PM: [INFO] collections removed: 0
    07-Apr-2008 05:45:01 PM: [INFO] collections recreated: 0
    07-Apr-2008 05:45:01 PM: [INFO] collections fetched (minimize this): 1
    07-Apr-2008 05:45:01 PM: [INFO] queries executed to database: 21
    07-Apr-2008 05:45:01 PM: [INFO] query cache puts: 0
    07-Apr-2008 05:45:01 PM: [INFO] query cache hits: 0
    07-Apr-2008 05:45:01 PM: [INFO] query cache misses: 0
    07-Apr-2008 05:45:01 PM: [INFO] max query time: 485ms
    I guess spring cannot close a session thats been forced open?

  2. #12
    Join Date
    Apr 2008
    Posts
    21

    Default

    I think I have to get out of the idea of it being multiple threads giving me issues with the sf.getCurrentSession(). I stuck code in the jsp, manager and DAO to give me Thread info and here was what I got.

    Code:
    code:
    System.out.println("JSP thread: " + (java.lang.management.ManagementFactory.getThreadMXBean().getThreadInfo(Thread.currentThread().getId())).toString());
    
    results:
    JSP thread: Thread http-443-Processor24 (Id = 61) RUNNABLE null
    Manager thread: Thread http-443-Processor24 (Id = 61) RUNNABLE null
    DAO thread: Thread http-443-Processor24 (Id = 61) RUNNABLE null

  3. #13
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    I guess spring cannot close a session thats been forced open?
    Spring cannot close sessions that it doesn't know of, or in other words session opened by you should be closed by you. I.e. don't use openSession....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #14
    Join Date
    Apr 2008
    Posts
    21

    Default

    Yea finally understand that too. I am somewhat stuck on where to go with this. I cannot find any meaningful reason as to why it is not working. I have proof that im in the same thread the entire time, I get given a sessionFactory that works fine forcing open sessions, and I have Transactions working correctly. I am currently re-reading the spring and hibernate ref's looking for clues. I am close to compiling spring with debug lines turned on so I can trace the thing better. Is there some sessionFactory property I have missed? Does having a custom datasource based off AbstractDataSource do anything to the sessionFactory?

    Is there more areas I can debug?

    [edit] the forum update email said you wanted the spring debug, here is as much as I can paste:
    Code:
    08-Apr-2008 02:41:07 PM: [TRACE] Getting BeanInfo for class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]
    08-Apr-2008 02:41:07 PM: [TRACE] Caching PropertyDescriptors for class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'cacheableMappingLocations' of type [[Lorg.springframework.core.io.Resource;]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'class' of type [java.lang.Class]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'collectionCacheStrategies' of type [java.util.Properties]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'configLocation' of type [org.springframework.core.io.Resource]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'configuration' of type [org.hibernate.cfg.Configuration]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'configurationClass' of type [java.lang.Class]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'dataSource' of type [javax.sql.DataSource]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'entityCacheStrategies' of type [java.util.Properties]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'entityInterceptor' of type [org.hibernate.Interceptor]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'eventListeners' of type [java.util.Map]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'exposeTransactionAwareSessionFactory' of type [boolean]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'filterDefinitions' of type [[Lorg.hibernate.engine.FilterDefinition;]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'hibernateProperties' of type [java.util.Properties]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'jtaTransactionManager' of type [javax.transaction.TransactionManager]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'lobHandler' of type [org.springframework.jdbc.support.lob.LobHandler]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'mappingDirectoryLocations' of type [[Lorg.springframework.core.io.Resource;]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'mappingJarLocations' of type [[Lorg.springframework.core.io.Resource;]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'mappingLocations' of type [[Lorg.springframework.core.io.Resource;]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'mappingResources' of type [[Ljava.lang.String;]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'namingStrategy' of type [org.hibernate.cfg.NamingStrategy]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'object' of type [java.lang.Object]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'objectType' of type [java.lang.Class]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'schemaUpdate' of type [boolean]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'singleton' of type [boolean]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'typeDefinitions' of type [[Lorg.springframework.orm.hibernate3.TypeDefinitionBean;]
    08-Apr-2008 02:41:07 PM: [TRACE] Found bean property 'useTransactionAwareDataSource' of type [boolean]
    08-Apr-2008 02:41:07 PM: [TRACE] Converting String to [interface org.springframework.core.io.Resource] using property editor [org.springframework.core.io.ResourceEditor@ad5fab]
    08-Apr-2008 02:41:07 PM: [INFO] Hibernate 3.2.5
    08-Apr-2008 02:41:07 PM: [INFO] hibernate.properties not found
    08-Apr-2008 02:41:07 PM: [INFO] Bytecode provider name : cglib
    08-Apr-2008 02:41:07 PM: [INFO] using JDK 1.4 java.sql.Timestamp handling
    08-Apr-2008 02:41:07 PM: [INFO] configuring from url: file:/C:/programs/Tomcat5/common/classes/hibernate.cfg.xml
    08-Apr-2008 02:41:07 PM: [INFO] Reading mappings from resource : xxx.hbm.xml
    08-Apr-2008 02:41:07 PM: [INFO] Mapping class: xxx -> xxx
    08-Apr-2008 02:41:08 PM: [INFO] Reading mappings from resource : xxxService.hbm.xml
    08-Apr-2008 02:41:08 PM: [INFO] Mapping class: xxxService -> xxx_service
    08-Apr-2008 02:41:08 PM: [INFO] Configured SessionFactory: null
    08-Apr-2008 02:41:08 PM: [INFO] Building new Hibernate SessionFactory
    08-Apr-2008 02:41:08 PM: [INFO] Mapping collection: xxx.services -> xxx_service
    08-Apr-2008 02:41:08 PM: [INFO] Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
    08-Apr-2008 02:41:08 PM: *** Active=0/22 Idle=1/0/5
    08-Apr-2008 02:41:08 PM: *** xxxDataSource.setUsers(): user from ThreadLocal=superuser;
    08-Apr-2008 02:41:08 PM: [INFO] RDBMS: sql server, version: 12.50
    08-Apr-2008 02:41:08 PM: [INFO] JDBC driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase, version: 1.2
    08-Apr-2008 02:41:08 PM: [INFO] Using dialect: org.hibernate.dialect.SybaseDialect
    08-Apr-2008 02:41:08 PM: [INFO] Using default transaction strategy (direct JDBC transactions)
    08-Apr-2008 02:41:08 PM: [INFO] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    08-Apr-2008 02:41:08 PM: [INFO] Automatic flush during beforeCompletion(): disabled
    08-Apr-2008 02:41:08 PM: [INFO] Automatic session close at end of transaction: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Scrollable result sets: enabled
    08-Apr-2008 02:41:08 PM: [INFO] JDBC3 getGeneratedKeys(): enabled
    08-Apr-2008 02:41:08 PM: [INFO] Connection release mode: on_close
    08-Apr-2008 02:41:08 PM: [INFO] Default batch fetch size: 1
    08-Apr-2008 02:41:08 PM: [INFO] Generate SQL with comments: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Order SQL updates by primary key: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Order SQL inserts for batching: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    08-Apr-2008 02:41:08 PM: [INFO] Using ASTQueryTranslatorFactory
    08-Apr-2008 02:41:08 PM: [INFO] Query language substitutions: {}
    08-Apr-2008 02:41:08 PM: [INFO] JPA-QL strict compliance: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Second-level cache: enabled
    08-Apr-2008 02:41:08 PM: [INFO] Query cache: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Cache provider: org.hibernate.cache.NoCacheProvider
    08-Apr-2008 02:41:08 PM: [INFO] Optimize cache for minimal puts: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Structured second-level cache entries: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Echoing all SQL to stdout
    08-Apr-2008 02:41:08 PM: [INFO] Statistics: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Deleted entity synthetic identifier rollback: disabled
    08-Apr-2008 02:41:08 PM: [INFO] Default entity-mode: pojo
    08-Apr-2008 02:41:08 PM: [INFO] Named query checking : enabled
    08-Apr-2008 02:41:08 PM: [INFO] building session factory
    08-Apr-2008 02:41:08 PM: [INFO] Not binding factory to JNDI, no JNDI name configured
    08-Apr-2008 02:41:08 PM: [INFO] Running schema validator
    08-Apr-2008 02:41:08 PM: [INFO] fetching database metadata
    08-Apr-2008 02:41:08 PM: *** Active=0/22 Idle=1/1/5
    08-Apr-2008 02:41:08 PM: *** xxxDataSource.setUsers(): user from ThreadLocal=superuser;
    08-Apr-2008 02:41:08 PM: [INFO] table found: xxx
    08-Apr-2008 02:41:08 PM: [INFO] columns: [lots of columns]
    08-Apr-2008 02:41:08 PM: [INFO] table found: xxx_service
    08-Apr-2008 02:41:08 PM: [INFO] columns: [lots of columns]
    08-Apr-2008 02:41:09 PM: [TRACE] Getting BeanInfo for class [xxxDAOImpl]
    08-Apr-2008 02:41:09 PM: [TRACE] Caching PropertyDescriptors for class [xxxDAOImpl]
    08-Apr-2008 02:41:09 PM: [TRACE] Found bean property 'class' of type [java.lang.Class]
    08-Apr-2008 02:41:09 PM: [TRACE] Found bean property 'dataSource' of type [javax.sql.DataSource]
    08-Apr-2008 02:41:09 PM: [TRACE] Found bean property 'sessionFactory' of type [org.hibernate.SessionFactory]
    08-Apr-2008 02:41:09 PM: [DEBUG] Eagerly caching bean 'xxxManager' to allow for resolving potential circular references
    08-Apr-2008 02:41:09 PM: [DEBUG] Opening Hibernate Session
    08-Apr-2008 02:41:09 PM: [DEBUG] Closing Hibernate Session
    08-Apr-2008 02:41:09 PM: [DEBUG] Returning cached instance of singleton bean 'DataSource'
    Last edited by gman; Apr 8th, 2008 at 01:45 PM.

  5. #15
    Join Date
    Mar 2008
    Posts
    8

    Default

    Hi Marten,

    Can you post the URLs to ur post where you were saying:
    1) DON'T do transaction management yourself, let spring do it.
    2) DON'T do session management yourself, let spring do it.

    Thanks?

  6. #16
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #17
    Join Date
    Apr 2008
    Posts
    21

    Default

    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?

  8. #18
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #19
    Join Date
    Apr 2008
    Posts
    21

    Default 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •