JDBC 确保事务成功的方法
时间:2021-07-01 10:21:17
帮助过:7人阅读
package com.shop.impl;
2
3 import com.shop.util.ConfigManager;
4
5 import java.sql.*
;
6
7
8 public class BaseDao {
9 PreparedStatement ps =
null;
10 ResultSet rs =
null;
11 static Connection conn =
null;
12 private static final String driver = ConfigManager.getInstance().getString("jdbc.driver"
);
13 private static final String url = ConfigManager.getInstance().getString("jdbc.url"
);
14 private static final String user = ConfigManager.getInstance().getString("jdbc.userName"
);
15 private static final String password = ConfigManager.getInstance().getString("jdbc.password"
);
16
17
18 /**
19 * @return connection 静态方法,确保该方法的Connection对象是不可变的
20 * @author 周小龙
21 */
22 public static Connection getConn() {
23 try {
24 Class.forName(driver);
25 conn =
DriverManager.getConnection(url, user, password);
26 // System.out.println(conn);
27 }
catch (ClassNotFoundException e) {
28 e.printStackTrace();
29 }
catch (SQLException e) {
30 e.printStackTrace();
31 }
32 return conn;
33 }
34
35
36 /**
37 * @param sql
38 * @param obj
39 * @return
40 * @author 查询方法
41 */
42 public ResultSet executeQuery(String sql, Object... obj) {
43 getConn();
44 try {
45 ps =
conn.prepareStatement(sql);
46 if (obj !=
null) {
47 for (
int i = 0; i < obj.length; i++
) {
48 ps.setObject((i + 1
), obj[i]);
49 }
50 }
51 rs =
ps.executeQuery();
52 }
catch (SQLException e) {
53 e.printStackTrace();
54 }
55 return rs;
56
57 }
58
59 /**
60 * 更新方法
61 * @param sql
62 * @param obj
63 * @return
64 */
65 public int executeUpdate(String sql, Object... obj) {
66 getConn();
67 int result = 0
;
68 try {
69 ps =
conn.prepareStatement(sql);
70 if (obj !=
null) {
71 for (
int i = 0; i < obj.length; i++
) {
72 ps.setObject((i + 1
), obj[i]);
73 }
74 try {
75 result =
ps.executeUpdate();
76 }
catch (Exception e) {
77 System.out.println("JDBC操作失误!"
);
78 }
79 }
80 }
catch (SQLIntegrityConstraintViolationException e) {
81
82 }
catch (SQLException e) {
83 e.printStackTrace();
84 }
finally {
85 closeAll();
86 }
87 return result;
88 }
89
90 /**
91 * @param conn Connection 对象,在所有的子类里面都可以直接通过getConn()方法获得
92 * @param sql sql 语句 这个方法其实和普通的增改删除方法只差一个connection对象
93 * @param obj Object对象
94 * @return 专门用来进行事务的方法 为确保事务的成功,必须在同一个connection对象里面操作
95 * @author 9527
96 */
97 public boolean transactionUpdate(Connection conn, String sql, Object... obj) {
98 Connection connection =
conn;
99 boolean flag =
false;
100 try {
101 ps =
conn.prepareStatement(sql);
102 if(obj!=
null){
103 for (
int i = 0; i < obj.length; i++
) {
104 ps.setObject((i+1
),obj[i]);
105 }
106 }
107 int row =
ps.executeUpdate();
108 if (row>0
){
109 flag=
true;
110 }
else {
111 flag=
false;
112 }
113 }
catch (SQLException e) {
114 e.printStackTrace();
115 }
116 return flag;
117 }
118
119 /**
120 * 释放资源
121 */
122 public void closeAll() {
123 try {
124 if (rs !=
null) {
125 rs.close();
126 }
127 if (ps !=
null) {
128 ps.close();
129 }
130 if (conn !=
null) {
131 conn.close();
132 }
133 }
catch (SQLException e) {
134 e.printStackTrace();
135 }
136 }
137 }
涉及事务的BaseDao---在普通的BaseDao的基础上增加了专门处理事务的方法
1 /**
2 * @param conn 连接对象
3 * @param user 买家对象
4 * @param money 金额 事务:转账,转入
5 * @author 周小龙
6 */
7 @Override
8 public int transferIn(Connection conn, User user, double money) {
9 Connection connection = conn;
10 String sql = "UPDATE `user` set user_balance=user_balance+? WHERE userId=? ";
11 Object[] obj = {money, user.getUserId()};
12 int result = this.executeUpdate(sql, obj);
13 return result;
14 }
15
16 /**
17 * @param conn 连接对象
18 * @param user 买家对象
19 * @param money 金额 事务转账,转出
20 * @author 周小龙
21 */
22 @Override
23 public int transferOut(Connection conn, User user, double money) {
24 Connection connection=conn;
25 String sql = "UPDATE `user` set user_balance=user_balance-? WHERE userId=? ";
26 Object[] obj = {money, user.getUserId()};
27 int result = this.executeUpdate(sql, obj);
28 return result;
29 }
继承了BaseDao的卖家类的Impl的关键代码,买家类也是一样的写法
1 /**
2 * 售货交易
3 * @author 周小龙
4 * @param seller
5 * @param user
6 * @param money
7 * @return
8 */
9 public boolean transferBuy(Seller seller, User user, double money) {
10 conn=BaseDao.getConn();
11 boolean flag=false;
12 try {
13 //关闭数据库的自动提交
14 conn.setAutoCommit(false);
15 //转账 买家转出,卖家转入
16 int out = userDao.transferOut(conn,user,money);
17 int in = sellerDao.transferIn(conn,seller,money);
18 //如果转账成功,就提交事务,flag赋值为true
19 if (out>0&&in>0){
20 conn.commit();
21 flag=true;
22 }
23 } catch (SQLException e) {
24 e.printStackTrace();
25 //如果报错 就回滚操作,并且给flag赋值为false
26 try {
27 conn.rollback();
28 } catch (SQLException ex) {
29 ex.printStackTrace();
30 }
31 }finally {
32 try {
33 conn.setAutoCommit(true);
34 } catch (SQLException e) {
35 e.printStackTrace();
36 }
37 }
38 return flag;
39 }
service层的事务的关键代码
JDBC 确保事务成功的方法
标签:back dstat 子类 config 报错 oid gets public 连接