2

我是 Spring 框架的新手,我一直在尝试在我的代码中使用事务。我在我的方法上使用@Transactional,并且在 beans.xml 中设置了正确的标签。看起来@transactional 标记被忽略了。任何帮助表示赞赏。

另外,我使用的是tomcat,启动时没有看到任何错误。

也使用 Maven。

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.abhi</groupId>
    <artifactId>quote</artifactId>
    <name>quote</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.7</java-version>
        <org.springframework-version>3.2.3.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>   

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-hibernate3</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

豆类.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.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.abhi.quote.config"></context:component-scan>

    <context:property-placeholder
        location="classpath:com/abhi/quote/config/jdbc.properties" />



    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="username" value="${jdbc.username}"></property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven />

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml
        classpath:com/abhi/quote/beans/beans.xml
        classpath:com/abhi/quote/config/security-context.xml
        classpath:com/abhi/quote/config/service-context.xml
        classpath:com/abhi/quote/config/dao-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/spring/appServlet/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>
  <filter>
    <display-name>springSecurityFilterChain</display-name>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <description>MySQL Quote</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/quote</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

</web-app>

doa-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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.abhi.quote.dao"></context:component-scan>

<jee:jndi-lookup jndi-name="jdbc/quote" id="dataSource"
    expected-type="javax.sql.DataSource">
</jee:jndi-lookup>

</beans>

使用事务的代码片段:

package com.abhi.quote.dao;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component("usersDao")
public class UsersDao {

private NamedParameterJdbcTemplate jdbc;

    public UsersDao(){
    System.out.println("Loaded");
    }

    @Autowired
    public void setDataSource(DataSource jdbc){
        this.jdbc = new NamedParameterJdbcTemplate(jdbc);
    }

    @Transactional
    public boolean create(User user){

        DebugUtils.transactionRequired("UsersDao.create");
        BeanPropertySqlParameterSource userParams = new BeanPropertySqlParameterSource(user);

        jdbc.update("insert into users (username, password, email, enabled)" +
                " values (:username, :password, :email, :enabled)", userParams);

        return jdbc.update("insert into authorities (username, authority) values (:username, :authority)", userParams) == 1;
    }

}

调用 create 方法的服务层代码

package com.abhi.quote.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.abhi.quote.dao.User;
import com.abhi.quote.dao.UsersDao;

@Service("userService")
public class UserService {

private UsersDao usersDao;

@Autowired
public void setUsersDao(UsersDao usersDao){
    this.usersDao = usersDao;
}

public void create(User user){
    usersDao.create(user);
}

}

DebugUtils 类:我正在使用这个类来检查是否加载了事务支持。它说没有加载支持

package com.abhi.quote.dao;

import java.lang.reflect.InvocationTargetException;

class DebugUtils {

private static final boolean transactionDebugging = true;
private static final boolean verboseTransactionDebugging = true;

public static void showTransactionStatus(String message) {
    System.out.println(((transactionActive()) ? "[+] " : "[-] ") + message);
}

// Some guidance from: http://java.dzone.com/articles/monitoring-declarative-transac?page=0,1
public static boolean transactionActive() {
    try {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        Class tsmClass = contextClassLoader.loadClass("org.springframework.transaction.support.TransactionSynchronizationManager");
        Boolean isActive = (Boolean) tsmClass.getMethod("isActualTransactionActive", null).invoke(null, null);

        return isActive;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

    // If we got here it means there was an exception
    throw new IllegalStateException("ServerUtils.transactionActive was unable to complete properly");
}

public static void transactionRequired(String message) {
    // Are we debugging transactions?
    if (!transactionDebugging) {
        // No, just return
        return;
    }

    // Are we doing verbose transaction debugging?
    if (verboseTransactionDebugging) {
        // Yes, show the status before we get to the possibility of throwing an exception
        showTransactionStatus(message);
    }

    // Is there a transaction active?
    if (!transactionActive()) {
        // No, throw an exception
        throw new IllegalStateException("Transaction required but not active [" + message + "]");
    }
}
}
4

4 回答 4

1

很大程度上取决于第二次插入是如何失败的。

默认情况下,只有在使用@Transactional 标记的方法抛出 RuntimeException 时,Spring 才会对事务进行回滚。

如果发生其他任何事情(抛出已检查的异常,或者方法成功完成),则事务将被提交。

我相信 SQLExceptions 是异常,而不是 RuntimeExceptions,因此您要么必须捕获 SQLException 并抛出 RuntimeException,要么重新配置事务注释(例如 @Transactional(rollbackFor=Exception.class) )

例如,参见http://simplespringtutorial.com/springDeclarativeTransactions.html

于 2013-10-26T15:32:22.737 回答
1

除了 GreyBeardedGeek 所说的,您正在访问的 DAO 是一个类,而不是一个接口。所以你应该设置

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

并将 CGLIB 添加到您的类路径中,否则事务注释将不起作用。

于 2013-10-26T15:40:33.177 回答
1

你应该移动这个

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="username" value="${jdbc.username}"></property>
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <property name="dataSource" ref="dataSource"></property>
</bean>

<tx:annotation-driven />

doa-context.xml删除jndi查找数据源(或给每个DataSource一个idTransactionManager只能将事务管理应用于引用的DataSource。所以选择你想要的。

这是必要的,因为当您在 中指定多个上下文时contextConfigLocation context-param,每个上下文都会单独刷新,然后将它们合并。因此tx:annotation-driven,一个上下文文件不适用于其他上下文文件。

于 2013-10-26T18:08:03.607 回答
0

除了@GreyBeardedGeek、@Luciano 和@Sotirios 所说的,我犯了一个严重的错误。

我使用 STS 为我创建了一个 Web 项目。

因此,调度程序被指示从我的控制器所在的基本包“com.abhi.quote”中读取所有bean。

当我从那里继续时,我创建了名为“com.abhi.quote.dao”、“com.abhi.quote.service”等的包,并将它们放在 contextLoaderListner 中。

因此,根上下文和调度程序上下文(servlet 上下文)都在读取 bean,因为所有其他包都是基本包的子文件夹。

我在 dao-context.xml 中定义了 tx 注释驱动标签,该标签只能由根上下文读取。

当应用程序运行时,servlet 包中的 bean 被使用,因为它们是子级(servlet 上下文)中存在的 bean,并且事务注释没有被兑现。

这些帖子将帮助您更好地理解:

为什么 DispatcherServlet 创建另一个应用程序上下文?

Spring XML 文件配置层次结构帮助/说明

在父上下文与子上下文中声明 Spring Bean

希望这可以帮助。

于 2013-10-28T20:52:39.670 回答