事务概述
● 事务指的是逻辑上的一组操作,组成这组操作的各个单元要么全都成功,要么全都失败。
● 事务作用: 保证在一个事务中多次操作要么全部成功,要么全部失败。
转账操作:
代码实现:
Servlet层接收数据:
package com.transaction.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Utils.C3P0Utils;
import com.transaction.ImpService.IAccountService;
import com.transaction.Service.AccountService;
public class AccountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@SuppressWarnings("null")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//首先设置编码格式
request.setCharacterEncoding("utf-8");
//从jsp页面获得数据
String from = request.getParameter("from");
String to = request.getParameter("to");
String moneystr = request.getParameter("money");
//money应该是double(Integer)类型 所以进行判断强转
Integer money=null;
if(moneystr!=null||!moneystr.equals("")){
money=Integer.parseInt(moneystr);
}
System.out.println(from);
System.out.println(to);
System.out.println(money);
Connection connection=C3P0Utils.getConnection();
try {
connection.setAutoCommit(false);
//将获得的数据交给service层进行处理
IAccountService service=new AccountService();
int i = service.transaction(from,to,money,connection);
System.out.println(i);
response.setContentType("text/html;charset=utf-8");
if (i==0) {
//转账成功
response.getWriter().println("转账成功");
connection.commit();
}else {
response.getWriter().println("转账失败");
}
} catch (SQLException e) {
System.out.println("连接池异常");
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
接收数据后传递给service层进行业务逻辑处理:
package com.transaction.ImpService;
import java.sql.Connection;
public interface IAccountService {
int transaction(String from, String to, Integer money, Connection connection);
}
package com.transaction.Service;
import java.sql.Connection;
import java.sql.SQLException;
import com.transaction.Dao.AccountDao;
import com.transaction.ImpService.IAccountService;
public class AccountService implements IAccountService {
@Override
public int transaction(String from, String to, Integer money,Connection connection) {
int out=0;
int in=0;
try {
AccountDao accountDao=new AccountDao();
out = accountDao.outMoney(connection,from,money);
in = accountDao.inMoney(connection,to,money);
System.out.println(out);
System.out.println(in);
if (out==1&&in==1) {
return out-in;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 1;
}
}
service层处理完成后传递给dao层进行与数据库的连接处理:
package com.transaction.Dao;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import com.Utils.C3P0Utils;
public class AccountDao {
//获得连接池
QueryRunner qr=new QueryRunner(C3P0Utils.getDataSource());
public int outMoney(Connection con, String from, Integer money) throws SQLException {
//写sql语句
String sql="update account set money=money-? where name=?";
Object[] param={money,from};
return qr.update(con, sql, param);
}
public int inMoney(Connection con, String to, Integer money) throws SQLException {
String sql="update account set money=money+? where name=?";
Object[] param={money,to};
return qr.update(con, sql, param);
}
}
C3P0连接池:
package com.Utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Utils {
//使用默认配置
//private static ComboPooledDataSource data=new ComboPooledDataSource();
//使用命令配置
private static ComboPooledDataSource data=new ComboPooledDataSource("hxzyah");
//获得数据源(连接池)
public static DataSource getDataSource(){
return data;
}
//获得链接
public static Connection getConnection(){
try {
return data.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
C3P0-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--命令的配置 -->
<named-config name="hxzyah">
<!--连接数据库的4项基本参数 -->
<property name="driverClass">
com.mysql.jdbc.Driver
</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/scahien
</property>
<property name="user">root</property>
<property name="password">123456</property>
<!--如果池中数据连接不够是一次增长多少个 -->
<property name="acquireIncrement">5</property>
<!--初始化连接数 -->
<property name="initialPoolSize">20</property>
<!--最小连接数 -->
<property name="minPoolSize">10</property>
<!--最大连接数 -->
<property name="maxPoolSize">40</property>
<!--JDBC的标准参数,用以控制数据源内加载的PreperedStatements数量 -->
<property name="maxStatements">0</property>
<!--连接池内单个连接所拥有的最大缓存statements数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
实现结果: