Spring的transaction-manager的用法

2)transaction-manager:

例 2.2.2

注意配置文件头加了两条:spring-tx-3.0.xsd和xmlns:tx

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
       >
 
    <context:component-scan
        base-package="com" />
    <context:component-scan
        base-package="service" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="NiutDAO" class="com.NiutDAO">
    </bean>
    <bean id="loginService" class="service.LoginServiceImpl" >
        <property name="niutDAO">
            <ref bean="NiutDAO" />
        </property>
    </bean>
   
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="login*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置切面 这种写法也正确"execution(* service.*.*(..))"-->
    <aop:config>
        <aop:pointcut id="myPointcut" expression="execution(* service.LoginServiceImpl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>        
    </aop:config>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
         <property name="driverClassName"
                      value="com.mysql.jdbc.Driver"></property>
         <property name="url"
                      value="jdbc:mysql://localhost:3306/test"></property>
         <property name="username"
                      value="root"></property>
         <property name="password"
                      value="1234"></property>
    </bean>
    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>
     <bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

package service;
import java.sql.Types;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.NiutDAO;
import service.interfac.ILoginService;

public class LoginServiceImpl implements ILoginService {
    @Resource
    private JdbcTemplate jt;
    
    NiutDAO niutDAO;
    public void setNiutDAO(NiutDAO niutDAO) {
        this.niutDAO = niutDAO;
    }
    public void login() {
        updateRegister(15,1);
        System.out.println("successfully update 1");
        updateRegisterWrong(19,2);
        System.out.println("successfully update 2");

    }    
    public void updateRegister(int age,int id) {
        String sql = "UPDATE register SET age=? WHERE id=?";
        Object params[] = new Object[] { new Integer(age),
                 new Integer(id) };
        int type[] = new int[] { Types.INTEGER , Types.INTEGER };
        jt.update(sql, params, type);
    }
    public void updateRegisterWrong(int age,int id) {
        String sql = "UPDATE register SET age=? WWWWWWW id=?";
        Object params[] = new Object[] { new Integer(age),
                new Integer(id) };
        int type[] = new int[] { Types.INTEGER , Types.INTEGER };
        jt.update(sql, params, type);
    }
}

即使updateRegister(15,1);成功执行,但数据库中结果并没有被更新,因为updateRegisterWrong(19,2);的错误致使updateRegister(15,1);的结果发生了回滚。

输出结果:
successfully update 1
严重: Servlet.service() for servlet spring threw exception
java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WWWWWWW id=2' at line 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44591615/article/details/109206425

posted @ 2022-01-13 19:39  malala  阅读(334)  评论(0编辑  收藏  举报