spring - Unable to fix "could not obtain transaction-synchronized Session for current thread" -


i new spring hibernate. using sprin 4.3.8 , hibernate 5.2.

i tried solutions available on net , have of them in code, yet getting error please me resolve this.

error:

exception in thread "main" org.hibernate.hibernateexception: not obtain transaction-synchronized session current thread @ org.springframework.orm.hibernate5.springsessioncontext.currentsession(springsessioncontext.java:133) @ org.hibernate.internal.sessionfactoryimpl.getcurrentsession(sessionfactoryimpl.java:456) @ com.travello.daoimpl.activitydaoimpl.getactivity(activitydaoimpl.java:43) @ com.travello.model.springmain.main(springmain.java:17) 

here activitylistdaoimpl:

@transactional public class activitydaoimpl implements activitydao {  @autowired private sessionfactory sessionfactory;    public sessionfactory getsessionfactory() {     return sessionfactory; }  public void setsessionfactory(sessionfactory sessionfactory) {     this.sessionfactory = sessionfactory; }  @override public activitylist getactivity(int activity_id) {     session session = sessionfactory.getcurrentsession();     activitylist activity = session.get(activitylist.class, activity_id);     return activity;     } } 

the main method:

public class springmain {   public static void main(string[] args) {      @suppresswarnings("resource")     applicationcontext context = new classpathxmlapplicationcontext         ("spring.xml");     activitydaoimpl dao = (activitydaoimpl) context.getbean("activitydao", activitydaoimpl.class);      activitylist activity = dao.getactivity(1);     system.out.println(activity.getactivityname());     system.out.println("done");   }  } 

the spring.xml

    <?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:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"     xsi:schemalocation="     http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">      <context:property-placeholder location="classpath:resources/database.properties" />     <context:annotation-config/>     <context:component-scan base-package="com.travello" />  <bean id="datasource"         class="org.springframework.jdbc.datasource.drivermanagerdatasource">         <property name="driverclassname" value="${database.driver}" />         <property name="url" value="${database.url}" />         <property name="username" value="${database.user}" />         <property name="password" value="${database.password}" />     </bean>       <bean id="sessionfactory" class="org.springframework.orm.hibernate5.localsessionfactorybean">         <property name="datasource" ref="datasource" />         <property name="configlocation" value="hibernate.cfg.xml" />     </bean>        <bean id="hibernatetransactionmanager"         class="org.springframework.orm.hibernate5.hibernatetransactionmanager">         <property name="sessionfactory" ref="sessionfactory" />     </bean>        <bean id="activitydao" class="com.travello.daoimpl.activitydaoimpl">         <property name="sessionfactory" ref="sessionfactory" />     </bean>      </beans> 

i have @transational annotation applied , bean same in spring.xml yet there's error.

add <tx:annotation-driven/> spring.xml. see here , here


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -