web19 事务
- mysql(有的数据库没有事务)默认一条sql代表一个自动事务(分自动事务和手动事务 )
- 建表有自增长则必须指定为主键
- 一条sql不需要事务,多条同时状态的需要事务
- 先写dao层,在写web层,最后结合复杂业务逻辑都在service层
- getparameter获取的都是string,转double(double.parsedouble(str))
- response的中文是(response.setcontenttype("text/html;charset='utf-8'"))记住中文是一个注意点
- 自建错误int =1/0;但不是sql错误捕获不到异常后果很可怕,改一处就要想相关影响
- 操作 事务的conn必须是同一个
- 回滚不代表提交也不代表结束,只有commit·代表结束因此不论有没有回滚都必须commit结束一个 事务(不过当所在方法结束会自动将事务放回池子,但下次可能会拿到这个没有结束事务的conn )
- 事务的隔离级别:乱读,读别人还未提交,改自己未完成事务数据;读别人已经提交改自己未完成事务数据;在自身事务未完成,不修改数据;自身未完成他人不可修改数据
1.事务
- 一件事情有n个组成单元 要不这n个组成单元同时成功 要不n个单元就同时失败就是将n个组成单元放到一个事务中
2.mysql的事务
默认的事务:一条sql语句就是一个事务 默认就开启事务并提交事务
手动
1)显示的开启一个事务:start transaction事务:
2)事务提交:commit代表从开启事务到事务提交 中间的所有的sql都认为有效 真正的更新数据库
3)事务的回滚:rollback 代表事务的回滚 从开启事务到事务回滚 中间的所有的 sql操作都认为无效数据库没有被更新
二、JDBC事务操作
默认是自动事务:
执行sql语句:executeUpdate() ---- 每执行一次executeUpdate方法 代表 事务自动提交
通过jdbc的API手动事务:
开启事务:conn.setAutoComnmit(false);
提交事务:conn.commit();
回滚事务:conn.rollback();
注意:控制事务的connnection必须是同一个
执行sql的connection与开启事务的connnection必须是同一个才能对事务进行控制
三、DBUtils事务操作
1.QueryRunner
有参构造:QueryRunner runner = new QueryRunner(DataSource dataSource);
有参构造将数据源(连接池)作为参数传入QueryRunner,QueryRunner会从连 接池中获得一个数据库连接资源操作数据库,所以直接使用无Connection参数 的update方法即可操作数据库
无参构造:QueryRunner runner = new QueryRunner();
无参的构造没有将数据源(连接池)作为参数传入QueryRunner,那么我们在使 用QueryRunner对象操作数据库时要使用有Connection参数的方法
四、使用ThreadLocal绑定连接资源
-----DBUtils的修改,将service,dao层的connection使用ThreadLocal联系起来确保同一个,同时使service得conn看不见---
-------web层
public class TransferServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String in = request.getParameter("in");
String out = request.getParameter("out");
Double money = Double.parseDouble(request.getParameter("money"));
Service ser = new Service();
boolean istranfer=ser.istransfer(in,out,money);
response.setContentType("text/html;charset=utf-8");
if (istranfer) {
response.getWriter().write("转账成功");
}else {
response.getWriter().write("转账失败");
}
}
--------------service层
public class Service {
public boolean istransfer(String in, String out, Double money) {
// Connection conn=null;
boolean istransfer=true;
Dao dao = new Dao();
try {
// ------service出现dao层的conn不合适所以将获得conn写进方法类中--------
// conn=C3p0Utiles.getCon();
// conn.setAutoCommit(false);
// dao.in( conn,in,money);
// int a=1/0;
// dao.out( conn,out,money);
// --------将当前线程绑定当前conn------------
MyCurrentConn.startCommit();
dao.in(in, money);
dao.out(out, money);
} catch (Exception e) {
istransfer=false;
try {
// conn.rollback();
MyCurrentConn.rollBackCommit();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
try {
// conn.commit();
MyCurrentConn.endCommit();
} catch (SQLException e) {
e.printStackTrace();
}
}
return istransfer;
}
}
-----------------dao层
public class Dao {
private QueryRunner qr = new QueryRunner();
/*public void in(Connection conn, String in, Double money) throws SQLException {
String sql="update count set money=money+? where name=?";
qr.update(conn, sql,money,in);
}
public void out(Connection conn, String out, Double money) throws SQLException {
String sql="update count set money=money-? where name=?";
qr.update(conn, sql,money,out); }*/
public void in( String in, Double money) throws SQLException {
String sql="update count set money=money+? where name=?";
qr.update(MyCurrentConn.getCurrentConn(), sql,money,in);
}
public void out( String out, Double money) throws SQLException {
String sql="update count set money=money-? where name=?";
qr.update(MyCurrentConn.getCurrentConn(), sql,money,out);
}
----------------最关键的工具-ThreadLocal仅能携带一个,想带多个将参数改成list
public class MyCurrentConn {
private static ComboPooledDataSource datasourse = new ComboPooledDataSource("text");
private static ThreadLocal<Connection> tl = new ThreadLocal<>();
public static ComboPooledDataSource getCombpdatasource() {
return datasourse;
}
public static Connection getCurrentConn() {
Connection conn = tl.get();
if (conn == null) {
conn = getCon();
tl.set(conn);
}
return conn;
}
public static void startCommit() throws SQLException {
getCurrentConn().setAutoCommit(false);
}
public static void rollBackCommit() throws SQLException {
getCurrentConn().rollback();
;
}
public static void endCommit() throws SQLException {
getCurrentConn().commit();
// 释放tl绑定资源,关闭close;
tl.remove();
getCurrentConn().close();
; }
public static Connection getCon() {
try {
return datasourse.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Paste_Image.png
Paste_Image.png
2.事务的特性和隔离级别(概念性问题---面试)
-
1.事务的特性ACID
1)原子性(Atomicity)原子性是指事务是一个不可分割的工作单位,事务中的操作 要么都发生,要么都不发生。
2)一致性(Consistency)一个事务中,事务前后数据的完整性必须保持一致。
3)隔离性(Isolation)多个事务,事务的隔离性是指多个用户并发访问数据库时, 一个用户的 事务不能被其它用户的事务所干扰,多个并发事务之间数据要相互隔离。
4)持久性(Durability)持久性是指一个事务一旦被提交,它对数据库中数据的改变 就是永久性的,接下来即使数据库发生故障也不应该对其有任何影响。 -
2.并发访问问题----由隔离性引起
如果不考虑隔离性,事务存在3中并发访问问题。
1)脏读:B事务读取到了A事务尚未提交的数据 ------ 要求B事务要读取A事 务提交的数据
2)不可重复读:一个事务中 两次读取的数据的内容不一致 ----- 要求的是一个事 务中多次读取时数据是一致的 --- unpdate
3)幻读/虚读:一个事务中 两次读取的数据的数量不一致 ----- 要求在一个事务多 次读取的数据的数量是一致的 --insert delete
3.事务的隔离级别
1)read uncommitted : 读取尚未提交的数据 :哪个问题都不能解决
2)read committed:读取已经提交的数据 :可以解决脏读 ---- oracle默认的
3)repeatable read:重读读取:可以解决脏读 和 不可重复读 ---mysql默认的
4)serializable:串行化:可以解决 脏读 不可重复读 和 虚读---相当于锁表
注意:mysql数据库默认的隔离级别
查看mysql数据库默认的隔离级别:select @@tx_isolation
设置mysql的隔离级别:set session transaction isolation level 设置事务隔离级别
Paste_Image.png
总结:
mysql的事务控制:
开启事务:start transaction;
提交:commit;
回滚:rollback;
JDBC事务控制:
开启事务:conn.setAutoCommit(false);
提交:conn.commit();
回滚:conn.rollback();
DBUtils的事务控制 也是通过jdbc
ThreadLocal:实现的是通过线程绑定的方式传递参数
概念:
事务的特性ACID
并发问题:脏读、不可重读、虚读\幻读
解决并发:设置隔离级别
read uncommitted
read committed
repeatable read (mysql默认)
serialazable
隔离级别的性能:
read uncommitted>read committed>repeatable read>serialazable
安全性:
read uncommitted<read committed<repeatable read<serialazable