user979051
user979051

Reputation: 1267

Spring 3.2 Hibernate No active transaction

I'm new to Spring, using version 3.2 with Hibernate 4.1.9Final, it seems that @Transactional annotations are being ignored, i've tried to set it on the controller method, service method and dao, no success

I've included the packages in

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > 
    <display-name>
      Spring
    </display-name>
    <description>
     Spring Test
    </description>

  <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>



  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>





</web-app>

springapp-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
                    http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/tx
                   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                   http://www.springframework.org/schema/aop
                   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <context:component-scan base-package="com.test.web.controllers,com.test.service.impl" />
     <context:annotation-config />
    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull"/>
    <property name="username" value="medi"/>
    <property name="password" value="tech"/>
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingLocations" value="classpath*:com/test/model/hbm/**/*.hbm.xml" />

    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
        hibernate.show_sql=true
        hibernate.current_session_context_class=thread
      </value>
    </property>
  </bean>

 <tx:annotation-driven transaction-manager="transactionManager" mode="proxy"/>
  <bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>



    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>


    <bean id="categoryDAO" class="com.test.dao.hibernate.HibernateCategoryDAO">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="categoryService" class="com.test.service.impl.Categories" scope="singleton">
        <property name="dao" ref="categoryDAO"></property>
    </bean>

</beans>

My Test Controller

public class HelloController {

    @Autowired
    private CategoryService categories;

    public HelloController() {
        System.out.println("test!!!");
    }

    public void setCategoryService(CategoryService categories) {
        this.categories = categories;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @Transactional
    public String getIndex() {
        Category c = new Category();
        c.setName("Test");
        categories.save(c);
        return "index";
    }
}

Stacktrace:

org.hibernate.HibernateException: save is not valid without active transaction
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)
    at $Proxy19.save(Unknown Source)
    at com.test.dao.hibernate.HibernateCategoryDAO.save(HibernateCategoryDAO.java:20)
    at com.test.service.impl.Categories.save(Categories.java:21)
    at com.test.web.controllers.HelloController.getIndex(HelloController.java:36)
    at com.test.web.controllers.HelloController$$FastClassByCGLIB$$aa12a3a3.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

Upvotes: 0

Views: 1796

Answers (3)

user979051
user979051

Reputation: 1267

Removed hibernate.current_session_context_class=thread and it works. Spring injecting its own session context managing implementation when using Spring transaction support layer?

Upvotes: 1

StanislavL
StanislavL

Reputation: 57381

Try to add <context:component-scan base-package="your package here">

Looks like it just can't find annotations

Upvotes: 0

shazin
shazin

Reputation: 21883

You are using @Transactional in the controller layer. It should be in your Service layer.

Make Categories.save method @Transactional and you might get this error away.

Upvotes: 0

Related Questions