javaWeb事务
JDBC事务:
cmd 命令上的事务开启: start transaction; / begin;
回滚 rollback;
提交 commit;
JDBC事务控制:
开启事务:conn.setAutoCommit(false);
提交:conn.commit();
回滚:conn.rollback();
DBUtils的事务控制 也是通过jdbc
ThreadLocal:实现的是通过线程绑定的方式传递参数
事务回滚后 ,一定要commit 才能算这个事务完成
jdbc事务的运用
form 提交表单
<fieldset>
<legend> 转账</legend>
<form action="${pageContext.request.contextPath}/transferServlet" method="post">
转出账户:<input type="text" name="outName"><br/><br>
转入账户:<input type="text" name="inName"><br><br>
转账金额 :<input type="text" name="money"><br/><br>
<input type="submit" value="确定"> </form>
</fieldset>
web 层 servlet doGet 和doPost 方法:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String inName=request.getParameter("inName");
String outName=request.getParameter("outName");
String moneystr=request.getParameter("money");
double money=Double.parseDouble(moneystr);
//调用service层的方法
TransferService transfer=new TransferService();
boolean falg = transfer.transfermoney(inName,outName,money);
if (falg) {
response.getWriter().write("转账成功");
}else {
response.getWriter().write("转账失败");
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
service 层类:
package com.study.transfer.service; import java.sql.Connection;
import java.sql.SQLException; import com.study.transfer.dao.TransferDao;
import com.study.util.C3P0Utils; public class TransferService { public boolean transfermoney(String inName, String outName, double money) {
//调用dao层的inMoney 和 outMoney TransferDao dao=new TransferDao();
Connection conn=null;
//dao 层 的conn
//jdbc事务的处理 conn SQL语句的执行 和事务的提交,回滚需要时同一个conn;
boolean isTransfer=true;
int number1=-1;
int number2=-1;
try {
//缺点是 出现了dao层的conn连接 ,
conn=C3P0Utils.getConnection();
conn.setAutoCommit(false);
number1=dao.inMoney(conn,inName ,money);
// int a=1/0;
number2=dao.outMoney(conn,outName,money);
if (number1==-1 || number2==-1) {
//当sql语句的任何一条没有执行成功,就回滚的初始状态
conn.rollback();
}
} catch (Exception e) {
isTransfer=false;
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
conn.commit();
//把事务提交,结束,最后把conn返回给连接池
} catch (SQLException e) {
e.printStackTrace();
}
}
return isTransfer;
} }
dao层:
package com.study.transfer.dao; import java.sql.Connection;
import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import com.study.util.C3P0Utils; public class TransferDao { public int inMoney(Connection conn, String inName, double money) throws SQLException {
//Connection conn=C3P0Utils.getConnection();
QueryRunner qr=new QueryRunner();
String sql="update account set money=money+? where name=?";
int number=qr.update(conn, sql, inName,money);
return number;
} public int outMoney(Connection conn, String outName, double money) throws SQLException {
// Connection conn=C3P0Utils.getConnection();
QueryRunner qr=new QueryRunner();
String sql="update account set money=money-? where name=?";
int number =qr.update(conn, sql, outName,money);
return number;
}
}
DBUtils 事务提交对上面的代码进行了修改:
service层:
public class TransferService { public boolean transfermoney(String inName, String outName, double money) {
//调用dao层的inMoney 和 outMoney TransferDao dao=new TransferDao();
Connection conn=null;
boolean isTransfer=true;
int number1=-1;
int number2=-1;
try {
//开启事务
C3P0Utils.startTransaction();
number1=dao.inMoney(inName ,money);
number2=dao.outMoney(outName,money);
if (number1==-1 || number2==-1) {
//当sql语句的任何一条没有执行成功,就回滚的初始状态
conn.rollback();
}
} catch (Exception e) {
isTransfer=false;
try {
C3P0Utils.rollbackTransaction();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
C3P0Utils.commitTransaction();
//把事务提交,结束,最后把conn返回给连接池
} catch (SQLException e) {
e.printStackTrace();
}
}
return isTransfer;
}
dao层:
public class TransferDao { public int inMoney( String inName, double money) throws SQLException {
Connection conn=MyC3P0Utils.getCurrenConnection();
QueryRunner qr=new QueryRunner();
String sql="update account set money=money+? where name=?";
int number=qr.update(conn, sql, money,inName);
return number;
} public int outMoney( String outName, double money) throws SQLException {
Connection conn=MyC3P0Utils.getCurrenConnection();
QueryRunner qr=new QueryRunner();
String sql="update account set money=money-? where name=?";
int number =qr.update(conn, sql, money,outName);
return number;
} }
C3P0Utils 的代码改变:
public class C3P0Utils {
private static ComboPooledDataSource dataSource=new ComboPooledDataSource("login");
private static ThreadLocal<Connection > tl=new ThreadLocal<Connection>();
// ThreadLocal 只能存储一个变量
//可TreadLocal<List<Object>>
//获取当前线程的连接connection
public static Connection getCurrenConnection(){
Connection conn = tl.get();
if (conn==null) {
conn=getConnection();
tl.set(conn);
}
return conn;
}
//开启事务
public static void startTransaction() throws SQLException{
Connection conn=getCurrenConnection();
conn.setAutoCommit(false);
}
//回滚事务
public static void rollbackTransaction() throws SQLException{
getCurrenConnection().rollback();
}
//提交事务
public static void commitTransaction() throws SQLException{
Connection conn=getCurrenConnection();
conn.commit();
//将Connection从ThreadLocal移除
tl.remove();
conn.close(); }
public static DataSource getDataSource(){
return dataSource;
} public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
javaWeb事务的更多相关文章
- JavaWeb 之事务
什么是事务? 事务就是逻辑上的一组操作,组成事务的各个执行单元,操作要么全部成功,要么全部失败. 以转账为例: 张三给李四转账,张三扣1000,李四加1000; 加钱和扣钱两个操作组成了一个事务. 1 ...
- javaweb学习总结(三十八)——事务
一.事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐,对应于如下两条sql语句 update from account set mone ...
- 传智播客JavaWeb day11--事务的概念、事务的ACID、数据库锁机制、
1. 什么叫做事务? 2.默认情况下每一条sql语句都是一个事务,然后自动提交事务 ps:如果想多条语句占一个事务,则可以手动设置SetAutoCommit为false 3.关键字 start tr ...
- JavaWeb学习总结(十二)--事务
一.事务的介绍 1.1 什么是事务 银行转账!张三转10000块到李四的账户,这其实需要两条SQL语句: 给张三的账户减去10000元: 给李四的账户加上10000元. 如果在第一条SQL语句执行成功 ...
- 【JAVAWEB学习笔记】19_事务
事务 学习目标 案例-完成转账 一.事务概述 1.什么是事务 一件事情有n个组成单元 要不这n个组成单元同时成功 要不n个单元就同时失败 就是将n个组成单元放到一个事务中 2.mysql的事务 默认的 ...
- JavaWeb学习笔记七 事务
什么是事务?一件事情有n个组成单元 ,要么这n个组成单元同时成功,要么n个单元就同时失败.就是将n个组成单元放到一个事务中. mysql的事务 默认的事务:一条sql语句就是一个事务,默认就开启事务并 ...
- Javaweb学习笔记——(十八)——————事务、DBCP、C3P0、装饰者模式
事务 什么是事务? 转账: 1.给张三账户减1000元 2.给李四账户加1000元 当给张三账户减1000元之后,抛出了异常,这 ...
- JavaWeb学习(二十九)———— 事务
一.事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐,对应于如下两条sql语句 update from account set mone ...
- JavaWeb基础—JDBC(二)事务与批处理
一.批处理 这里给出PrepareStatement的示例,优点是可以发送预编译的SQL,缺点是SQL语句无法更换,但参数可以更换 批处理:多条语句的处理 mysql默认是关闭的,要打开需要在url后 ...
随机推荐
- iOS-时间戳(或date)转字符串
1.时间戳转字符串 ///时间戳转化为字符转0000-00-00 00:00 + (NSString *)time_timestampToString:(NSInteger)timestamp{ NS ...
- Jquery之isPlainObject源码分析
今天对Jquery中 isPlainObject 源码分析. 1. isPlainObject 方法的作用: 用来判断传入参数,是否是对象. 2. 源码分析:isPlainObject: funct ...
- Go笔记-结构体
[定义] type identifier struct{ field1 type1 field2 type2 ... } // 声明 var s identifier identifier.field ...
- 分组密码的工作模式--wiki
密码学中,块密码的工作模式允许使用同一个块密码密钥对多于一块的数据进行加密,并保证其安全性.[1][2] 块密码自身只能加密长度等于密码块长度的单块数据,若要加密变长数据,则数据必须先被划分为一些单独 ...
- 转换number为千分位计数形式js
JS实现转换千分位计数 350000.00-------350,000.00 var num=0;function format (num) { return (num.toFixed(2) + '' ...
- 四、正则表达式re模块
什么是正则表达式 正则表达式,又称规则表达式,通常被用来检索.替换那些符合某个模式(规则)的文本. 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一 ...
- HTTP入门
请求报文图解: 请求报文 图片 响应报文图解: 响应报文
- [Python Study Notes]CS架构远程访问获取信息--Client端v1.0
更新内容: 1.添加entry栏默认ip和port口 2.修正退出功能 3.添加退出自动关闭窗口功能 4.优化cpu显示为固定保留两位小数 '''''''''''''''''''''''''''''' ...
- install atom markdown preview plus error
Installing "markdown-preview-enhanced@0.15.2" failed.Hide output- npm ERR! Darwin 17.2.0 n ...
- dedecms文章页调用上一篇和下一篇文章
dedecms文章页调用上一篇和下一篇文章,解析后是链接形式的上下篇 {dede:prenext get='pre'/} {dede:prenext get='next'/}