0

I have two insert operation in one http post request. something like

@Service
public class StudentService {

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int create(final Student student) {
        // insert into table 1

        // insert into table 2 using id return from insert 1, but something BAD happen here
    }
}

so, I added @Transactional at method level as shown above.

In order to make it work, I added this tx:annotation-driven

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

so my app-servlet.xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <mvc:annotation-driven />
    <mvc:resources mapping="/res/**" location="/res/" />

    <context:component-scan base-package="com.app" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="dataSource" name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://xxx:3306/AppDb" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>

the component-scan will basically scan all class include controller, service, model, viewmodel, helper class etc.

If I remove the tx:annotation-driven, the @transactional won't work. But once I add this tx:annotation-driven, some error starts to appear, like

Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.app.service.StudentService com.app.controller.HomeController.studentRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService' defined in file [/path/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MathSchool/WEB-INF/classes/com/app/service/StudentService.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/cglib/transform/impl/MemorySafeUndeclaredThrowableStrategy

How can I make this @transactional work.

Jens
  • 67,715
  • 15
  • 98
  • 113
Timeless
  • 7,338
  • 9
  • 60
  • 94
  • possible duplicate of [How to solve this weird error with Tomcat, Spring and Maven?](http://stackoverflow.com/questions/18361136/how-to-solve-this-weird-error-with-tomcat-spring-and-maven) – Jens Apr 17 '15 at 09:51
  • @Jens I have spring-core jar included. – Timeless Apr 17 '15 at 09:54
  • Do you have also spring-aop included? – Jens Apr 17 '15 at 10:02
  • @Jens Yes, spring-aop-3.2.13.RELEASE – Timeless Apr 17 '15 at 10:02
  • You have to add the same version of spring aop as your spring core is. – Jens Apr 17 '15 at 10:05
  • @Jens Because I'm using spring-security-web 3.2.7.RELEASE, the aop 3.x is automatically added. – Timeless Apr 17 '15 at 10:10
  • Use spring-security-web version 4.0.0.RELEASE. That will be compatible with spring core 4.1.6.RELEASE – Jens Apr 17 '15 at 10:11
  • @Jens I didn't upgrade spring security to version 4, because more materials, question/answer on version 3, I need to study the migration as well which takes more time. I downgrade my spring to 4.0. – Timeless Apr 17 '15 at 10:26

1 Answers1

0

Thanks to @Jens

My project using spring 4.1.6 and spring security 3.2.7

According to this documentation Migrating from Spring Security 3.x to 4.x (XML Configuration)

Spring Security 4 now requires Spring 4 (Spring 4.0 or 4.1, I guess). Conveniently, Spring Security 3.2.x works with Spring 3.2.x and Spring 4 (Spring 4.0 Only, I guess).

The only thing I did is downgrade Spring 4.1 to Spring 4.0

Community
  • 1
  • 1
Timeless
  • 7,338
  • 9
  • 60
  • 94