Results 1 to 7 of 7

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

  1. #1
    Join Date
    Apr 2010
    Posts
    5

    Default 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 at 12:33 PM. Reason: changing txManager to HibernateTransactionManager

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Use [ code][/code ] tags when posting code...

    Configure the correct transactionmanager, you are using hibernate so use the hibernatetransactionmanager.
    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

  3. #3
    Join Date
    Apr 2010
    Posts
    5

    Default

    thanks, will give that a shot.

  4. #4
    Join Date
    Apr 2010
    Posts
    5

    Default

    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.

  5. #5
    Join Date
    Apr 2010
    Posts
    5

    Default

    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.

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

    Default

    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...
    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. #7
    Join Date
    Apr 2010
    Posts
    5

    Default

    ah, ok. I'm still learning.

Posting Permissions

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