d6d4b43f000eafe359a3d656307b06920e4a7b21
01.\345\274\200\345\217\221\346\211\213\345\206\214/06.\345\270\270\350\247\201\351\227\256\351\242\230QA/03.meta\344\270\212\344\270\213\346\226\207\344\275\277\347\224\250\350\247\204\350\214\203.md
... | ... | @@ -12,27 +12,14 @@ |
12 | 12 | |
13 | 13 | ```java |
14 | 14 | |
15 | - /** |
|
16 | - * 第1步:使用try-with-resources new Meta(),会自动调用meta的close方法 |
|
17 | - * |
|
18 | - * <pre> |
|
19 | - * @Override |
|
20 | - * public void close(Meta meta) { |
|
21 | - * // 回滚异常 |
|
22 | - * if (meta.getError()) { |
|
23 | - * meta.rollback(); |
|
24 | - * } |
|
25 | - * // 提交事物 |
|
26 | - * connection.commit(); |
|
27 | - * // 关闭连接 |
|
28 | - * connection.close(); |
|
29 | - * } |
|
30 | - * </pre> |
|
31 | - */ |
|
32 | - @MethodService(description = "创建Meta") |
|
33 | - public void newMeta() { |
|
15 | + //1.正确的写法:一定要使用try-catch-finally语法 在finally close |
|
16 | + @MethodService(description = "创建用户", name = "newMeta") |
|
17 | + public void newMeta(String id, int age, int times) { |
|
18 | + |
|
19 | + Meta meta = null; |
|
20 | + try { |
|
21 | + meta = new Meta(Meta.SUPERUSER, new HashMap<>()); |
|
34 | 22 | |
35 | - try (Meta meta = new Meta(Meta.SUPERUSER, new HashMap<>())) { |
|
36 | 23 | // 第2步:设置meta到线程变量 |
37 | 24 | BaseContextHandler.setMeta(meta); |
38 | 25 | /** |
... | ... | @@ -42,21 +29,58 @@ |
42 | 29 | * |
43 | 30 | * |
44 | 31 | */ |
45 | - |
|
46 | 32 | // 第4步:刷新所有的数据到数据库 |
47 | 33 | meta.flush(); |
34 | + |
|
48 | 35 | } catch (Exception e) { |
49 | - e.printStackTrace(); |
|
50 | - Meta meta = BaseContextHandler.getMeta(); |
|
51 | - // 第5步设置为异常,调用默认close方法的时候根据getError回滚 |
|
36 | + // 第5步:异常设置setError=true,会回滚 |
|
52 | 37 | if (meta != null) { |
53 | 38 | meta.setError(true); |
54 | 39 | } |
40 | + throw e; |
|
55 | 41 | } finally { |
42 | + // 第6步:调用close关闭连接和commit |
|
43 | + if (meta != null) { |
|
44 | + meta.close(); |
|
45 | + } |
|
56 | 46 | // 第6步:清理线程变量 |
57 | 47 | BaseContextHandler.remove(); |
58 | 48 | } |
59 | - |
|
60 | 49 | } |
61 | 50 | |
51 | +``` |
|
52 | + |
|
53 | + |
|
54 | +```java |
|
55 | + |
|
56 | + |
|
57 | + //2.错误的写法,不能使用:try-with-resources,这种会导致事物不会回滚。 |
|
58 | + |
|
59 | + @MethodService(description = "创建用户",name = "newMetaNoRollback") |
|
60 | + public void newMetaNoRollback(String id, int age,int times) { |
|
61 | + |
|
62 | + try (Meta meta = new Meta(Meta.SUPERUSER, new HashMap<>())) { |
|
63 | + // 第2步:设置meta到线程变量 |
|
64 | + BaseContextHandler.setMeta(meta); |
|
65 | + /** |
|
66 | + * TODO第3步:处理自己的业务逻辑 TestRole role = new TestRole(); role.setRoleName("test"); role.set("remark", "测试"); |
|
67 | + * role.create(); |
|
68 | + * |
|
69 | + * |
|
70 | + * |
|
71 | + */ |
|
72 | + loopData(id, age, times); |
|
73 | + |
|
74 | + // 第4步:刷新所有的数据到数据库 |
|
75 | + meta.flush(); |
|
76 | + } catch (Exception e) { |
|
77 | + e.printStackTrace(); |
|
78 | + BaseContextHandler.getMeta().setError(true); |
|
79 | + throw e; |
|
80 | + } finally { |
|
81 | + // 第6步:清理线程变量 |
|
82 | + BaseContextHandler.remove(); |
|
83 | + } |
|
84 | + |
|
85 | + } |
|
62 | 86 | ``` |
... | ... | \ No newline at end of file |