Announcement Announcement Module
No announcement yet.
Getting different Sessions-Objects with OSIV Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting different Sessions-Objects with OSIV

    I have a problem implementinmg OSIV right.
    I am using GenericDAO with OSIV, and a SessionFactory.
    Everytime getSession in a DAO is called during the same request, a different Session Object is used and I dont know why. Normally the SessionFactory should prevent this, right? (Every operation within the DAO's calls get Session e.g. to create criteria).

    This is what my getSession() Method in the DAO looks like:
    	protected Session getSession() {
    		if (session == null || !session.isOpen()){"Session not set or closed");
    				this.session = sessionFactory.getCurrentSession();"SessionFactory: "+sessionFactory.hashCode());"CurrentSession: "+session.hashCode());
    			} catch (NullPointerException e) {
    				logger.error("SessionFactory nicht gesetz!");
    				throw new IllegalStateException("Session has not been set on DAO before usage");
    		return session;
    The logged hashcode from the SessionFactory is always the same, but different for every Session.

    ApplicationContext (some parts are stripped):
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref local="dataSource" />
    		<property name="hibernateProperties">
    				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
    				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    				<prop key="">${}</prop>
    				<prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
    				<prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
    				<prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
    				<prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
    				<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
    				<prop key="hibernate.c3p0.idle_test_period">${hibernate.c3p0.idle_test_period}</prop>
    				<prop key="hibernate.c3p0.acquire_increment">${hibernate.c3p0.acquire_increment}</prop>
    				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    				<prop key="hibernate.cache.use_query_cache">false</prop>
    				<prop key="hibernate.max_fetch_depth">10</prop>				
    				<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
    				<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>								
    		<property name="mappingResources">
    		<bean id="geschaeftspartnerDAO" class="app.dao.impl.GeschaeftspartnerDAOHibernate" scope="singleton" >
    			<property name="sessionFactory" ref="sessionFactory" />			
    		<bean id="standortAdministration" class="app.administration.StandortAdministration" scope="singleton">
    			<property name="standortDAO" ref="standortDAO" />	
    Here is the definition of the OSIV-Filter in web.xml:
    			Implementierung des OpenSessionInView-Pattern.
    			Funktioniert, warum auch immer, nur mit Mapping auf die Request-URL.
    			Mapping auf das FacesServlet führt scheinbar dazu, dass der Filter nicht aufgerufen wird.
    		<servlet-name>Persistent Faces Servlet</servlet-name>
    	<!-- ICEFaces Servlets -->
    		<servlet-name>Persistent Faces Servlet</servlet-name>
    There are no exceptions during startup of Tomcat, so I excpect everything to be configured correct, at least syntactically.

    Is there any fault in wiring-up the components?

    Tomcat 6.x
    ICEfaces 1.7, MyFaces 1.2
    Spring 2.5.5
    Hibernate 3.x

    Thx in advance.

  • #2
    Your Dao is flawed. You are keeping state in a Singleton NEVER do this.

    Your getSession method should look like this.

    protected Session getSession() {
      return sessionFactory.getCurrentSession();
    Never assign the Session to an instance variable, you only have 1 instance!!! Now imagine 30 threads operating on that single instance?!

    Also make sure that your filter mapping is correct, which Servlet is processing your requests? If that isn't the 'Persistent Faces Servlet' then the filter isn't doing anything.

    Finally make sure that you have some sort of transactional configuration, I don't see a HibernateTransactionManager and neither any of the further transactional configuration.


    • #3
      Thx for that advice so far, didnt see the forest full of trees ;-)
      Just copied the code from somewhere else and did not check for the session being held as attribute or retrieved every time from the SessionFactory.

      The Mapping is correct as 'Persistent Faces Servlet' is the name for the ICEfaces Servlet. I stripped the transaction config for better readability, in fact, Im using Springs declarative transaction management with annotations.

      One Problem remains and I'm sure it has something to do with transaction handling: All getter methods are working, but if I try to make an entity persistent, the corresponding method is not called and execution just stops without throwing an exception.

      This is my Businessmethod:
          public void saveGP(Geschaeftspartner geschaeftspartner){
      Here is the Corresponding DAO:
      public class GeschaeftspartnerDAOHibernate 
      	extends GenericHibernateDAO<Geschaeftspartner, Long> 
      	implements GeschaeftspartnerDAO{
      	Logger logger = Logger.getLogger(this.getClass());
      //  ... other methods 
      	public Geschaeftspartner save(Geschaeftspartner entity) {
      		return makePersistent(entity);
      finally the makePersistent() Method from the abstract GenericDAO superclass:
      	public T makePersistent(T entity) {
      		logger.debug("Make Persistent!");
      		logger.debug("Entity: "+entity);
      		return entity;
      When saveGP() is called, save() is invoked and the logging output "save()" created, but neither the logging-output from makePersistent(), nor the debug-output "TEST2" is written, so it seems like makePersistent() is not called.

      There is no @Transactional(readOnly=true) anywhere, so I have no Idea why persisting an item doesnt work...


      • #4
        just found out that invoking getSession().saveOrUpdate() within the DAO implementation does the trick. But why isnt it working when a method with the same statement (and inherited from the abstract superclass GenericHibernateDAO) is called? Annotating the method with @Transactional
        changes nothing...


        • #5
          The @Transactional should be on your service, also putting @Transactional on a method which is called internally isn't going to do anything (chapter 6 of the reference guide explains why).

          Also are they using the same logger instance and are the log levels set correctly?


          • #6
            I changed the DAO's get SessionMethod now to

            	protected Session getSession() {
            		Session newSession = sessionFactory.getCurrentSession();
    "Session: "+newSession.hashCode());
            		return newSession;
            Lazy loading is still not possible, since there are 2 Different Sessions during one request. Looks like the SessionFactory misses to deliver the right session.

            I get a org.hibernate.LazyInitializationException everytime LazyLoading is used. Setting the attribute to lazy=true within the mapping solves the problem, but i want to uses the lazy mechanism here.

            Any idea?


            • #7
              OK, i think I found something. Everything works fine if the filter-mapping is changed to

              So it looks like the Persistent Mapping Servlet is not working as expected.