-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: PROBLEM in No Hibernate Session bound to thread
PostPosted: Tue Feb 27, 2007 4:00 am 
Newbie

Joined: Mon Jan 29, 2007 11:14 am
Posts: 1
I am using Tomcat 5.5.20 Server, Spring, Hibernate 3.0 and Drools. I am tried to integrate all this three Spring-Hibernate-Drools.
The MVC application will be at Spring and the MVC application is having the form for the user inputs. After user inputs, it will be validated in the DROOLS (.drl file) and if it matches, I am calling the hibernate from the drools to insert the record in the database. I have done it successfully. But while updating I am getting the folowing error:

ERROR MESSAGE
Code:
INFO: Server startup in 24872 ms
It's Endowment Policy -- My First Rule is Fired Successfully!!
VALUE ::com.example.PolicyDAOImpl@796e1c
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
   at org.springframework.orm.hibernate3.LocalSessionFactoryBean$TransactionAwareInvocationHandler.invoke(LocalSessionFactoryBean.java:1067)
   at $Proxy0.getCurrentSession(Unknown Source)
   at com.example.PolicyDAOImpl.updatePolicyInfo(PolicyDAOImpl.java:34)
   at com.example.Rule_My_First_Rule_0.consequence(Rule_My_First_Rule_0.java:11)
   at com.example.Rule_My_First_Rule_0ConsequenceInvoker.evaluate(Rule_My_First_Rule_0ConsequenceInvoker.java:22)
   at org.drools.common.DefaultAgenda.fireActivation(Unknown Source)
   at org.drools.common.DefaultAgenda.fireNextItem(Unknown Source)
   at org.drools.common.AbstractWorkingMemory.fireAllRules(Unknown Source)
   at org.drools.common.AbstractWorkingMemory.fireAllRules(Unknown Source)
   at com.example.PolicyController.onSubmit(PolicyController.java:35)
   at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:356)
   at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
   at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:249)
   at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
   at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
   at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
   at java.lang.Thread.run(Thread.java:595)


CODING
Code:
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.utils.HibernateUtil;

public class PolicyDAOImpl  extends HibernateDaoSupport implements PolicyDAO {

   public void addPolicy(PolicyBean policybean) {
      getHibernateTemplate().save(policybean);
      
   }

   public PolicyBean getPolicyInfo(String policyNo) {
      PolicyBean policybean = null;
      String query = "from PolicyBean policybean where policybean.policyNo = ?";
      List list = getHibernateTemplate().find(query,policyNo);
      if(list.size() > 0){
         policybean = (PolicyBean)  list.get(0);
      }
      return policybean;
   }
   
   public void updatePolicyInfo(PolicyBean policybean) {
   
               
        Session session = getSessionFactory().getCurrentSession();
        session.beginTransaction();
        String hqlUpdate = "update PolicyBean set policyName = ? where policyId = ?";
       
        int updatedEntities = session.createQuery( hqlUpdate )
        .setString( 0, policybean.getPolicyName())
        .setLong(1, policybean.getPolicyId())
        .executeUpdate();
        session.getTransaction().commit();             
   }
}


Spring Configuration xml with Hibernate cnofiguration also
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!--  Application context definition for "spring MVC" DispatcherServlet.  -->
<beans>
   <!-- default handlermapping -->
   <bean id="beanNameUrlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

   <bean name="/policy.htm" class="com.example.PolicyController">
      <property name="rulebase">
         <ref bean="ruleBase"/>
      </property>      
      <property name="formView">
         <value>input</value>
      </property>
      <property name="successView">
         <value>welcome</value>
      </property>      
      <property name="validator">
         <bean class="com.example.PolicyValidator"/>
      </property>      
      <property name="policyDAOTar">
         <ref bean="policyDAOTarget"/>
      </property>               
   </bean>

   <!-- View Resolver -->
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
         <value>/WEB-INF/jsp/</value>      
      </property>
      <property name="suffix">
         <value>.jsp</value>
      </property>      
   </bean>
   
   <!-- Bean -->   
   
   <bean id="policyBean" class="com.example.PolicyBean"/>
   <!--  To Create RuleBase - Drools -->

   <bean id="ruleBase" class="com.example.RuleBaseBeanFactory">
      <property name="drlResourceList">
         <list>
            <value type="org.springframework.core.io.Resource">classpath:/com/example/Approval.drl</value>                     
         </list>
      </property>

      <property name="packageBuilderConfiguration">
         <bean class="org.drools.compiler.PackageBuilderConfiguration">
            <property name="javaLanguageLevel" value="1.5"/>
         </bean>
      </property>
   </bean>   

   <!--  JDBC Properties (key/value) -->

   <!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
         <list>
            <value>classpath:/com/example/jdbc.properties</value>
         </list>
      </property>
   </bean>-->
   
   <!--  To Create Datasource -->
   
   <bean id="sampleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="url">
         <value>jdbc:oracle:thin:@10.106.50.250:1521:IP</value>
      </property>
      <property name="driverClassName">
         <value>oracle.jdbc.driver.OracleDriver</value>
      </property>
      <property name="username">
         <value>ipdba</value>
      </property>
      <property name="password">
         <value>Chennai123</value>
      </property>
   </bean>       
   
   <!--  Hibernate Configuration -->   
   
   <bean id="sampleHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="properties">
         <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
            <prop key="hibernate.query.substitutions">true 'T', false 'F'</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.c3p0.minPoolSize">5</prop>
            <prop key="hibernate.c3p0.maxPoolSize">20</prop>
            <prop key="hibernate.c3p0.timeout">600</prop>
            <prop key="hibernate.c3p0.max_statement">50</prop>
            <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
         </props>
      </property>
   </bean>
   
   <!-- To Create Hibernate Session Factory -->
   
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource">
         <ref local="sampleDataSource"/>
      </property>
      <property name="hibernateProperties">
         <ref bean="sampleHibernateProperties"/>
      </property>
      <property name="mappingDirectoryLocations">
         <list>
            <value>classpath:/com/example</value>
         </list>
      </property>
   </bean>

   <bean id="policyDAOTarget" class="com.example.PolicyDAOImpl">
      <property name="sessionFactory">
         <ref local="sessionFactory"/>
      </property>
   </bean>


</beans>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 7:44 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi michealjohn,

Register your method in the transaction .It automatically closed your current session and removed it from current thread. So one way is to use all hibernate call between begin Transaction and close or you could use transaction Interceptor that provide you to start transaction from particular method. inside that method you could implement all the business logic save update. At the end of method it will take care of transaction

Before going to do so Plz.. understand about transaction .

_________________
Dharmendra Pandey


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.