Announcement Announcement Module
Collapse
No announcement yet.
OSIV, session closed problem (second set of eyes please) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OSIV, session closed problem (second set of eyes please)

    Hi. I know this is a common problem. I've read the forum posts regarding this, searched the web, tried multiple approaches but haven't hit the solution. My thoughts are I might be missing an annotation for @Transactional, OSIV is set up incorrectly, or something with Struts2 and OSIV is off. Would appreciate any feedback, have spent hours on this moving code around. Learned a lot but can't get the solution.

    Eclipse console is showing the OSIV Debug messages (e.g. "DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp")

    The error is "failed to lazily initialize a collection of role: com.lindoro.model.fbuilder.Strategy.formulae, no session or session was closed".

    The data is grabbed correctly with "(fetch=FetchType.EAGER)"

    Help would be very appreciated.

    ----------------------------------

    Strategy.java
    Code:
    @Entity
    @Table(name="strategies")
    public class Strategy {
    	
    	@Id
    	@GeneratedValue
    	@Column(name="id")
    	private int id;
    	
    	  @ManyToMany
    	  @JoinTable(name = "strategies_to_formulae",
    	    joinColumns = {
    	      @JoinColumn(name="strategy_id", unique = true)           
    	    },
    	    inverseJoinColumns = {
    	      @JoinColumn(name="formula_id")
    	    }
    	  )
    	private List<Formula> formulae;
    <snip>

    Formula.java
    Code:
    @Entity
    @Table(name="formulae")
    public class Formula {
    
    	@Id
    	@GeneratedValue(strategy = GenerationType.AUTO)
    	@Column(name="id")
    	private int id;
    	
    	@Column(name="ui_name")
    	private String uiName;
    <snip>
    StrategyDaoJdbc.java
    Code:
    @Transactional
    public class StrategyDaoJdbc  implements StrategyDao {
    
        private SessionFactory sessionFactory;
     
    	public StrategyDaoJdbc(SessionFactory sessionFactory) {
                this.sessionFactory = sessionFactory;
        }
    	
    	public List<Strategy> getStrategyList() {
            return sessionFactory.getCurrentSession().createQuery("from Strategy").list();
    	}
    <snip>
    ApplicationContext.xml
    Code:
         <bean id="strategyDao" class="com.lindoro.repository.fbuilder.StrategyDaoJdbc">
            <constructor-arg ref="sessionFactory"/>
        </bean>
    
    <bean id="sessionFactory"
    	class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    	<property name="dataSource" ref="dataSource" />
    	<property name="annotatedClasses">
    		<list>
    			<value>com.lindoro.model.fbuilder.Strategy</value>
    			<value>com.lindoro.model.fbuilder.Formula</value>
    		</list>
    	</property>
    	<property name="hibernateProperties">
    		<props>
    			<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
    			<prop key="hibernate.show_sql">true</prop>
    
    		</props>
    	</property>
    </bean>
    
    	<tx:annotation-driven transaction-manager="txManager"/>
    
       <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
      </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>

    web.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    	id="WebApp_ID" version="2.5">
    
    	<display-name>lindoro</display-name>
        <!-- FILTERS -->  
    	<filter>
    		<filter-name>struts2</filter-name>
    		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    	</filter>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
       
       <!-- FILTER MAPPINGS -->
    	<filter-mapping>
    		<filter-name>struts2</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	<!-- WELCOME FILES -->
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    	<!-- TAGLIBS -->
    	<jsp-config>
    		<taglib>
    			<taglib-uri>/WEB-INF/lib/struts-tags.tld</taglib-uri> 
    			<taglib-location>/WEB-INF/taglib/struts-tags.tld</taglib-location> 
    		</taglib>
    	</jsp-config>
    	
    	<!-- Hibernate OpenSession Filter -->
    <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
    <param-name>singleSession</param-name>
    <param-value>true</param-value>
    </init-param>
    <init-param>
    <param-name>sessionFactoryBeanName</param-name>
    <param-value>sessionFactory</param-value> 
    </init-param>
    </filter>
    
    <filter-mapping>
            <filter-name>hibernateFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>
    </web-app>
    Last edited by lindoro; May 2nd, 2010, 01:33 PM. Reason: changing txManager to HibernateTransactionManager

  • #2
    Use [ code][/code ] tags when posting code...

    Configure the correct transactionmanager, you are using hibernate so use the hibernatetransactionmanager.

    Comment


    • #3
      thanks, will give that a shot.

      Comment


      • #4
        Have updated the txmanager to use hibernatetransactionmanager. Still getting the same error message.

        I'm thinking there is something obvious I'm missing. As I continue to search for an answer, any feedback or things to try is great.

        Comment


        • #5
          Moving the HibernateFilter lines to the top of web.xml was the answer.

          DataSourceTransactionManager will work as well. I appreciate the HibernateTransactionManager comment as that's what I should be using.

          Comment


          • #6
            The transactionmanager doesn't work, it works due to auto commit mode but the DataSourceTransactionManager doesn't work with Hibernate... It seems to work due to the OpenSessionInViewFilter...

            Comment


            • #7
              ah, ok. I'm still learning.

              Comment

              Working...
              X