1

私は真剣にSpringのトランザクションで頭を悩ませています。@Transactional を機能させるために何日も検索しました。どんな助けでも大歓迎です!

MySQL データベースにアクセスする Web アプリに Spring 3.2 を使用しています (Spring MVC、JDBC、および Security を使用しています)。mysql テーブルは InnoDB を使用しています。宣言型トランザクションを機能させるのに苦労しています (プログラムによるトランザクションを使用してみましたが、正常に機能しました)。@Transactional で注釈が付けられたサービス メソッドを呼び出すコントローラーがあります。コントローラーはサーブレット xml ファイルで構成され、サービス/dao は別のアプリケーション xml ファイルで構成されます。@Transactional を取得してトランザクションを作成するための適切な構成が見つからないようです。エラーはありません。トランザクションがないだけです。ここに私のセットアップの詳細があります:

  1. 注釈付きコントローラーを使用して、サーブレット XML ファイルで構成された DispatcherServlet を使用する Web アプリケーション。コントローラーには Service インターフェースが注入されています。

  2. サービス メソッドには @Transactional のアノテーションが付けられ、注入された DAO クラスを呼び出します。

  3. サービス クラスには @Named のアノテーションが付けられ、アプリは component-scan を使用して spring-app-context.xml でこれらを取得するように構成されています。

  4. トランザクション構成は spring-app-context.xml でセットアップされます (サーブレット構成ファイルではありません)。

web.xml:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    /WEB-INF/config/spring-app-context.xml
    /WEB-INF/config/spring-security-context.xml
  </param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
  <servlet-name>appServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/spring-servlet-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>


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

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <!-- @Controller, @Service, @Configuration, etc. -->
    <context:component-scan base-package="ewarrants.web" />

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

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

    <context:property-placeholder location="/WEB-INF/config/ewarrants.properties"/>

</beans>

コントローラーのセットアップ例を次に示します。

@Inject
IUserService userService;

@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processRegistration(@Valid @ModelAttribute UserRegistrationVM incomingVM, BindingResult result, Model model) throws Exception {       

    ...

    userService.registerNewUser(newUser);

    return "redirect:<location>";
}

spring-app-context は次のとおりです。

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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">


    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <!-- @Controller, @Service, @Configuration, etc. -->
    <context:component-scan base-package="ewarrants.core" />
    <tx:annotation-driven transaction-manager="txManager" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="<url>"/>
        <property name="username" value="<user>"/>
        <property name="password" value="<password>"/> 
    </bean>

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

    <context:property-placeholder location="/WEB-INF/config/ewarrants.properties"/>
</beans>

サンプル サービスの実装を次に示します。

@Named
public class UserService extends Service implements IUserService {

    @Inject
    private IUserDao userDao;

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void registerNewUser(User user) throws Exception {
        ...

        // add the user record
        int newUserID = userDao.insertUser(user);

        // carry out more database inserts
    }
}
4

0 に答える 0