d9dfa8a55e01f8765b8dc146e60575bb5f632aeb
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
| ... | ... | @@ -1,5 +1,62 @@ |
| 1 | -1.Meta上下文是平台传参的核心,千万别滥用,业务系统不得自行new Meta(),如已使用,请检查后去掉,优先改掉启动事件方法里的代码 |
|
| 1 | +### 1.Meta上下文是平台传参的核心,千万别滥用,业务系统不得自行new Meta(),如已使用,请检查后去掉,优先改掉启动事件方法里的代码 |
|
| 2 | 2 | |
| 3 | -2.当同步方法中需要meta时,应该通过BaseContextHandle.getMeta()来获取上下文信息; |
|
| 4 | 3 | |
| 5 | -3.当异步方法中需要meta时,应该调用rs.callAsync,不得自行创建线程 |
|
| ... | ... | \ No newline at end of file |
| 0 | +### 2.当同步方法中需要meta时,应该通过BaseContextHandle.getMeta()来获取上下文信息; |
|
| 1 | + |
|
| 2 | + |
|
| 3 | +### 3.当异步方法中需要meta时,应该调用rs.callAsync,不得自行创建线程 |
|
| 4 | + |
|
| 5 | + |
|
| 6 | +### 4.创建自己的 new Meta() |
|
| 7 | + |
|
| 8 | + |
|
| 9 | +```java |
|
| 10 | + |
|
| 11 | + /** |
|
| 12 | + * 第1步:使用try-with-resources new Meta(),会自动调用meta的close方法 |
|
| 13 | + * |
|
| 14 | + * <pre> |
|
| 15 | + * @Override |
|
| 16 | + * public void close(Meta meta) { |
|
| 17 | + * // 回滚异常 |
|
| 18 | + * if (meta.getError()) { |
|
| 19 | + * meta.rollback(); |
|
| 20 | + * } |
|
| 21 | + * // 提交事物 |
|
| 22 | + * connection.commit(); |
|
| 23 | + * // 关闭连接 |
|
| 24 | + * connection.close(); |
|
| 25 | + * } |
|
| 26 | + * </pre> |
|
| 27 | + */ |
|
| 28 | + @MethodService(description = "创建用户") |
|
| 29 | + public void newMeta() { |
|
| 30 | + |
|
| 31 | + try (Meta meta = new Meta(Meta.SUPERUSER, new HashMap<>())) { |
|
| 32 | + // 第2步:设置meta到线程变量 |
|
| 33 | + BaseContextHandler.setMeta(meta); |
|
| 34 | + /** |
|
| 35 | + * TODO第3步:处理自己的业务逻辑 TestRole role = new TestRole(); role.setRoleName("test"); role.set("remark", "测试"); |
|
| 36 | + * role.create(); |
|
| 37 | + * |
|
| 38 | + * |
|
| 39 | + * |
|
| 40 | + */ |
|
| 41 | + |
|
| 42 | + // 第4步:刷新所有的数据到数据库 |
|
| 43 | + meta.flush(); |
|
| 44 | + } catch (Exception e) { |
|
| 45 | + e.printStackTrace(); |
|
| 46 | + Meta meta = BaseContextHandler.getMeta(); |
|
| 47 | + // 第5步设置为异常,调用默认close方法的时候根据getError回滚 |
|
| 48 | + if (meta != null) { |
|
| 49 | + meta.setError(true); |
|
| 50 | + } |
|
| 51 | + } finally { |
|
| 52 | + // 第6步:清理线程变量 |
|
| 53 | + BaseContextHandler.remove(); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | +``` |
|
| ... | ... | \ No newline at end of file |