事务处理的三种⽅式(简单⼩Demo)概述:事务指的是逻辑上的⼀组操作,组成这组操作的各个逻辑单元要么⼀起成功,要么⼀起失败.
mysql.sql
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
inrt into account values (null,'张三',10000);
inrt into account values (null,'凤姐',10000);
JDBC中事务的管理:
Dbutils实现事务管理:
界⾯:
<%@ page language="java" contentType="text/html; chart=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loo.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; chart=UTF-8">
<title>Inrt title here</title>
</head>
<body>
<h1>转账页⾯</h1>
<form action="${ tPath }/这⾥写Servlet" method="post">
付款⼈:<input type="text" name="from"><br/>
收款⼈:<input type="text" name="to"><br/>
转账⾦额:<input type="text" name="money"><br/>
<input type="submit" value="转账">
</form>
</body>
</html>
jar包:
⽅式⼀:
把多个对于数据库的操作所有的Connection绑定到当前线程中。该业务的操作所⽤到的Connection全部由当前线程管理。JDBCUtilsShiwu.java(jdbc⼯具类)
package com.it.utils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
hange.v2.c3p0.ComboPooledDataSource;
/**
* JDBC的⼯具类:把多个对于数据库的操作所有的Connection绑定到当前线程中。
该业务的操作所⽤到的Connection全部由当前线程管理
* @author admin
*/
public class JDBCUtilsShiwu {
private static final ComboPooledDataSource DATA_SOURCE =new ComboPooledDataSource(); private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
/**
* 获得连接的⽅法
*/
public static Connection getConnection(){
Connection conn = null;
try {
conn = tl.get();
if(conn == null){
conn = Connection();
tl.t(conn);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void beginTransaction() throws SQLException{
Connection conn = null;
conn = tl.get();
if(conn == null){
conn = Connection();
tl.t(conn);
}
conn.tAutoCommit(fal);
}
public static void commitTransaction() throws SQLException{
Connection conn = tl.get();
}
public static void rollBackTransaction() throws SQLException{
Connection conn = tl.get();
}
public static DataSource getDataSource(){
return DATA_SOURCE;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 默认配置 -->
<default-config>
<property name="driverClass"&sql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///day315</property>
<property name="ur">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
</c3p0-config>
Servlet02.java
package com.it.rvlet;
import java.io.IOException;
import javax.rvlet.ServletException;
import javax.rvlet.http.HttpServlet;
import javax.rvlet.http.HttpServletRequest;
import javax.rvlet.http.HttpServletRespon;
import com.it.rvices.Services02;
/**
* @author 侯青华
* @version 创建时间:2018年3⽉15⽇下午8:59:03
*/
public class Servlet02 extends HttpServlet {
private static final long rialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletRespon respon) throws ServletException, IOException { //解决中⽂乱码
request.tCharacterEncoding("utf-8");
//接收表单提交数据
String from = Parameter("from");
String to = Parameter("to");
double money = Double.Parameter("money"));
//调⽤rvice层处理
Services02 rvices02 = new Services02();
try {
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletRespon respon) throws ServletException, IOException { doGet(request, respon);
}
}
Services02.java
package com.it.rvices;
import java.sql.SQLException;
import com.it.dao.Dao02;
import com.it.utils.JDBCUtilsShiwu;
public class Services02 {
/**
* 处理⽅案⼆:思想:把多个对于数据库的操作所有的Connection绑定到当前线程中。该业务的操作所⽤到的Connection全部由当前线程管理 *
* @param from
* @param to
* @param money
* @throws Exception
*/
public void trans(String from, String to, double money) throws Exception {
Dao02 dao02 = new Dao02();
try {
//开启事务
JDBCUtilsShiwu.beginTransaction();
//处理业务
//扣钱
dao02.outmoney(from,money);
//加钱
dao02.inmoney(to,money);
//提交事务
} catch (SQLException e) {
//异常回滚事务
try {
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
Dao02.java