Skip to content

Commit 5a78ca5

Browse files
committed
update
1 parent df05de0 commit 5a78ca5

File tree

18 files changed

+219
-36
lines changed

18 files changed

+219
-36
lines changed

book/src/main/java/com/atguigu/dao/impl/BaseDao.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ public int update(String sql,Object...args){
3535
//4.发送SQL语句,并返回影响行数
3636
return queryRunner.update(connection,sql,args);
3737
} catch (SQLException e) {
38-
e.printStackTrace();
39-
}finally {
40-
//5.回收资源
41-
JdbcUtils.close(connection);
38+
e.printStackTrace();//捕获异常
39+
throw new RuntimeException(e);//抛出异常 给调用者 , 层层往上抛 , 这样上层才能回滚事务
4240
}
43-
return -1;
4441
}
4542

4643
/**
@@ -61,13 +58,10 @@ public<T> T queryForOne(Class<T>type, String sql,Object...args){
6158
//3.占位符赋值
6259
//4.发送SQL语句 并返回 结果集
6360
return queryRunner.query(connection,sql,new BeanHandler<T>(type),args);
64-
} catch (Exception e) {
65-
e.printStackTrace();
66-
}finally {
67-
//5.回收连接
68-
JdbcUtils.close(connection);
61+
} catch (SQLException e) {
62+
e.printStackTrace();//捕获异常
63+
throw new RuntimeException(e);//抛出异常 给调用者 , 层层往上抛 , 这样上层才能回滚事务
6964
}
70-
return null;
7165
}
7266

7367

@@ -89,12 +83,10 @@ public<T> List<T> queryForList(Class<T>type, String sql,Object...args){
8983
//3.占位符赋值
9084
//4.执行SQL语句 , 返回结果集
9185
return queryRunner.query(connection,sql,new BeanListHandler<T>(type),args);
92-
} catch (Exception e) {
93-
e.printStackTrace();
94-
}finally {
95-
JdbcUtils.close(connection);
86+
} catch (SQLException e) {
87+
e.printStackTrace();//捕获异常
88+
throw new RuntimeException(e);//抛出异常 给调用者 , 层层往上抛 , 这样上层才能回滚事务
9689
}
97-
return null;
9890
}
9991

10092
/**
@@ -111,11 +103,9 @@ public Object queryForSingleValue(String sql,Object...args){
111103
//3.占位符赋值
112104
//4.执行SQL语句,返回结果集
113105
return queryRunner.query(connection,sql,new ScalarHandler(),args);
114-
} catch (Exception e) {
115-
e.printStackTrace();
116-
}finally {
117-
JdbcUtils.close(connection);
106+
} catch (SQLException e) {
107+
e.printStackTrace();//捕获异常
108+
throw new RuntimeException(e);//抛出异常 给调用者 , 层层往上抛 , 这样上层才能回滚事务
118109
}
119-
return null;
120110
}
121111
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.atguigu.filter;
2+
3+
import com.atguigu.utils.JdbcUtils;
4+
5+
import javax.servlet.*;
6+
import java.io.IOException;
7+
8+
public class TransactionFilter implements Filter {
9+
@Override
10+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
11+
try {
12+
chain.doFilter(request, response);//
13+
JdbcUtils.commitAndClose();//没有异常就提交事务
14+
} catch (IOException e) {
15+
JdbcUtils.rollbackAndClose();//有异常就回滚事务
16+
e.printStackTrace();
17+
throw new RuntimeException();//把捕获的异常继续抛出 , 给到 tomcat服务器 来统一管理错误
18+
}
19+
}
20+
21+
@Override
22+
public void init(FilterConfig filterConfig) throws ServletException {
23+
24+
}
25+
26+
@Override
27+
public void destroy() {
28+
29+
}
30+
}

book/src/main/java/com/atguigu/service/impl/OrderServiceImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public String createOrder(Cart cart, Integer userId) {
3535
//保存订单
3636
orderDao.saveOrder(order);
3737

38+
//int i=12/0;//测试事务是否能正常回滚
39+
3840
//遍历购物车中每一个商品项,转换成订单项保存到数据库
3941
for(Map.Entry<Integer, CartItem> entry:cart.getItems().entrySet()){
4042
//取出每一个商品项

book/src/main/java/com/atguigu/test/JdbcUtilsTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
public class JdbcUtilsTest {
1010

11-
@Test
12-
public void testJdbcUtils(){
13-
for (int i = 0; i < 100; i++) {
14-
Connection connection = JdbcUtils.getConnection();
15-
System.out.println(connection);
16-
JdbcUtils.close(connection);
17-
}
18-
}
11+
// @Test
12+
// public void testJdbcUtils(){
13+
// for (int i = 0; i < 100; i++) {
14+
// Connection connection = JdbcUtils.getConnection();
15+
// System.out.println(connection);
16+
// JdbcUtils.close(connection);
17+
// }
18+
// }
1919

2020
}

book/src/main/java/com/atguigu/utils/JdbcUtils.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
public class JdbcUtils {
4040

4141
private static DataSource dataSource=null;//创建连接池对象
42+
private static ThreadLocal<Connection> conns=new ThreadLocal<Connection>();
4243

4344
static {
4445
//创建 配置文件对象
@@ -64,16 +65,67 @@ public class JdbcUtils {
6465
}
6566

6667
public static Connection getConnection() {
67-
Connection connection=null;
68-
//获取连接
69-
try {
70-
connection=dataSource.getConnection();
71-
} catch (SQLException e) {
72-
e.printStackTrace();
68+
Connection conn=conns.get();
69+
if (conn==null){
70+
try {
71+
conn=dataSource.getConnection();//如果当前线程中没有连接,则从连接池里取一个连接
72+
conns.set(conn);//保存到ThreadLocal线程本地变量中 , 供jdbc使用
73+
conn.setAutoCommit(false);//将 事 务 设置为手动管理
74+
} catch (SQLException e) {
75+
e.printStackTrace();
76+
}
7377
}
74-
return connection;
78+
return conn;
7579
}
7680

81+
/** 提交事务 并 关闭连接
82+
*
83+
*/
84+
public static void commitAndClose() {
85+
Connection connection = conns.get();
86+
if(connection!=null){
87+
try {
88+
connection.commit();//先 尝试提交
89+
} catch (SQLException e) {
90+
e.printStackTrace();
91+
} finally {
92+
try {
93+
connection.close();//无论有没有错误 , 都关闭连接
94+
} catch (SQLException e) {
95+
e.printStackTrace();
96+
}
97+
}
98+
}
99+
//一定要执行 remove 操作 ,因为tomcat服务器 底层使用了线程池
100+
conns.remove();
101+
102+
}
103+
104+
/** 回滚事务 并 关闭连接
105+
*
106+
*/
107+
public static void rollbackAndClose() {
108+
Connection connection = conns.get();
109+
if(connection!=null){
110+
try {
111+
connection.rollback();// 回滚
112+
} catch (SQLException e) {
113+
e.printStackTrace();
114+
} finally {
115+
try {
116+
connection.close();//无论有没有错误 , 都关闭连接
117+
} catch (SQLException e) {
118+
e.printStackTrace();
119+
}
120+
}
121+
}
122+
//一定要执行 remove 操作 ,因为tomcat服务器 底层使用了线程池
123+
conns.remove();
124+
}
125+
126+
127+
128+
/*
77129
public static void close(Connection connection){
78130
if(connection!=null){
79131
try {
@@ -84,6 +136,7 @@ public static void close(Connection connection){
84136
}
85137
86138
}
139+
*/
87140

88141

89142
}

book/src/main/java/com/atguigu/web/BaseServlet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
1919
//获取action业务 鉴别字符串,获取相应的业务 方法反射对象
2020
try {
2121
Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
22+
//这个方法是通过反射 调用业务方法的,即 servlet程序里的方法
23+
//而 所有的异常都是从Dao层抛到Service层,再抛到Web层里的Servlet方法
24+
//那Servlet接收到的异常抛到哪里? 没错 就是这个BaseServlet里
2225
method.invoke(this,req,resp);
2326
} catch (Exception e) {
2427
e.printStackTrace();
28+
throw new RuntimeException(e);//因为最终一定是由Filter过滤器来捕获异常,所以在任何一个地方捕获到异常都要向上抛出,绝对不能留着
2529
}
2630
}
2731
}

book/src/main/webapp/WEB-INF/web.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
<url-pattern>/orderServlet</url-pattern>
6363
</servlet-mapping>
6464

65+
66+
<!-- 后台资源过滤器 -->
6567
<filter>
6668
<filter-name>ManagerFilter</filter-name>
6769
<filter-class>com.atguigu.filter.ManagerFilter</filter-class>
@@ -72,4 +74,29 @@
7274
<!--不但要拦截 页面资源 ,还要拦截 Servlet访问路径-->
7375
<url-pattern>/manager/bookServlet</url-pattern>
7476
</filter-mapping>
77+
78+
<!-- 事务过滤器 , 用于给所有Service方法 提供 Commit和Rollback 事务 -->
79+
<filter>
80+
<filter-name>TransactionFilter</filter-name>
81+
<filter-class>com.atguigu.filter.TransactionFilter</filter-class>
82+
</filter>
83+
<filter-mapping>
84+
<filter-name>TransactionFilter</filter-name>
85+
<!-- 简单粗暴的 杠星 , 表示拦截所有资源请求,没错就是你工程里的所有资源 -->
86+
<url-pattern>/*</url-pattern>
87+
</filter-mapping>
88+
89+
<error-page>
90+
<!-- 错误类型代码 -->
91+
<error-code>500</error-code>
92+
<!-- 错误页面存放地址 -->
93+
<location>/pages/error/error500.jsp</location>
94+
</error-page>
95+
96+
<error-page>
97+
<!-- 错误类型代码 -->
98+
<error-code>404</error-code>
99+
<!-- 错误页面存放地址 -->
100+
<location>/pages/error/error404.jsp</location>
101+
</error-page>
75102
</web-app>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>404</title>
7+
<%-- 静态包含 base标签/css样式/jQuery文件 --%>
8+
<%@include file="/pages/common/head.jsp"%><style type="text/css">
9+
h1 {
10+
text-align: center;
11+
margin-top: 200px;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
17+
404 很抱歉,您访问的后台程序出现了错误 , 程序员小哥正在为您努力抢修!!! <br>
18+
<a href="index.jsp">返回首页</a>
19+
20+
21+
22+
<%-- 静态包含 页脚内容 --%>
23+
<%@include file="/pages/common/footer.jsp"%>
24+
</body>
25+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>500</title>
7+
<%-- 静态包含 base标签/css样式/jQuery文件 --%>
8+
<%@include file="/pages/common/head.jsp"%><style type="text/css">
9+
h1 {
10+
text-align: center;
11+
margin-top: 200px;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
17+
500 很抱歉,您访问的后台程序出现了错误 , 程序员小哥正在为您努力抢修!!! <br>
18+
<a href="index.jsp">返回首页</a>
19+
20+
21+
22+
<%-- 静态包含 页脚内容 --%>
23+
<%@include file="/pages/common/footer.jsp"%>
24+
</body>
25+
</html>
Binary file not shown.

0 commit comments

Comments
 (0)