前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring的transaction-manager的用法

Spring的transaction-manager的用法

作者头像
马克java社区
修改2021-05-20 14:27:41
3680
修改2021-05-20 14:27:41
举报
文章被收录于专栏:java大数据java大数据

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

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档