-->
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: Could not obtain transaction-synchronized Session
PostPosted: Thu Oct 23, 2014 9:05 am 
Newbie

Joined: Thu Oct 09, 2014 8:05 am
Posts: 6
I want to intergate hibernate-release-4.3.6 and spring-framework-4.1,my files is follows:
applicationContext.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">           
   
   <bean id="propertyConfigurer"
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
     p:location="/WEB-INF/jdbc.properties" />
   
   <bean id="dataSource"
     class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"
     p:driverClassName="${jdbc.driverClassName}"
     p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
     p:password="${jdbc.password}" />
     
  <bean id="sessionFactory"
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
      <property name="hibernateProperties">
         <props>
          <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
         </props>
      </property>
      
      <property name="packagesToScan">
         <list>
            <value>demo.entity</value>
         </list>
      </property>
   </bean>
   
   <bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:attributes>
         <tx:method name="insert*" propagation="REQUIRED" />
         <tx:method name="update*" propagation="REQUIRED" />
         <tx:method name="edit*" propagation="REQUIRED" />
         <tx:method name="save*" propagation="REQUIRED" />
         <tx:method name="add*" propagation="REQUIRED" />
         <tx:method name="new*" propagation="REQUIRED" />
         <tx:method name="set*" propagation="REQUIRED" />
         <tx:method name="remove*" propagation="REQUIRED" />
         <tx:method name="delete*" propagation="REQUIRED" />
         <tx:method name="change*" propagation="REQUIRED" />
         <tx:method name="get*" propagation="REQUIRED" read-only="true" />
         <tx:method name="find*" propagation="REQUIRED" read-only="true" />
         <tx:method name="load*" propagation="REQUIRED" read-only="true" />
         <tx:method name="*" propagation="REQUIRED" read-only="true" />
      </tx:attributes>
    </tx:advice>
   <context:component-scan base-package="demo.action" />
</beans>


hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
 
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="jdbc.batch_size">20</property>
    <property name="connection.autocommit">true</property>

    <property name="show_sql">true</property>
    <property name="connection.useUnicode">true</property>
    <property name="connection.characterEncoding">UTF-8</property>

   </session-factory>
</hibernate-configuration>


Java Code is follows:
Code:
package Demo.dao;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DemoDao<T>{
   //Session factory injected by spring context
   private SessionFactory sessionFactory;
   
   public SessionFactory getSessionFactory() { 
      return sessionFactory; 
   }
   
    @Autowired 
   public void setSessionFactory(SessionFactory sessionFactory) { 
      this.sessionFactory = sessionFactory; 
   }
   
   private Session getCurrentSession() { 
      return sessionFactory.getCurrentSession(); //---37 line
   }
   
    public List<T> find(String hql, Object[] param) { 
     Query q = this.getCurrentSession().createQuery(hql);   //----61 line
     if (param != null && param.length > 0) { 
       for (int i = 0; i < param.length; i++) { 
         q.setParameter(i, param[i]); 
       } 
     } 
     return q.list(); 
  } 
}


When I run above code,it raise following error. Where is wrong? how to correct it? Thanks.
Code:
Struts has detected an unhandled exception:
Messages: •Could not obtain transaction-synchronized Session for current thread
File: org/springframework/orm/hibernate4/SpringSessionContext.java
Line number: 134

Stacktraces
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    Demo.dao.DemoDao.getCurrentSession(DemoDao.java:37)
    Demo.dao.DemoDao.find(DemoDao.java:61)


Top
 Profile  
 
 Post subject: Re: Could not obtain transaction-synchronized Session
PostPosted: Fri Jan 02, 2015 8:29 am 
Newbie

Joined: Fri Jan 02, 2015 8:26 am
Posts: 1
Hi,

Please advise how were you able to resolve this issue.

Thank You!!
Regards
Amit


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.