From f2b3189e4ff77d70d2fd639d01b893453e9a51e2 Mon Sep 17 00:00:00 2001 From: "youHong.ai" <774495953@qq.com> Date: Wed, 21 Dec 2022 23:58:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E4=B8=80=E4=B8=AA=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AA=E4=BA=8B=E5=8A=A1mapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/aiyh/utils/Util.java | 22 +++++- .../aiyh/utils/recordset/RecordsetUtil.java | 12 +-- .../utils/recordset/RsThreadLocalManager.java | 75 +++++++++++++------ src/test/java/youhong/ai/pcn/UtilTest.java | 5 +- 4 files changed, 79 insertions(+), 35 deletions(-) diff --git a/src/main/java/aiyh/utils/Util.java b/src/main/java/aiyh/utils/Util.java index 1696df5..ed2e2e6 100644 --- a/src/main/java/aiyh/utils/Util.java +++ b/src/main/java/aiyh/utils/Util.java @@ -2,7 +2,9 @@ package aiyh.utils; import aiyh.utils.action.CusBaseAction; import aiyh.utils.annotation.*; +import aiyh.utils.annotation.recordset.SqlMapper; import aiyh.utils.entity.*; +import aiyh.utils.excention.BindingException; import aiyh.utils.excention.CustomerException; import aiyh.utils.fileUtil.ProperUtil; import aiyh.utils.mapUtil.UtilHashMap; @@ -1998,8 +2000,14 @@ public class Util extends weaver.general.Util { * @return boolean 是否提交成功 * @author youHong.ai ****************************************** */ - public static boolean commitTransMapper() { - return recordsetUtil.getRsManager().commit(); + public static boolean commitTransMapper(Class t) { + if (t == null) { + throw new NullPointerException("can not commit trans for null mapper proxy!"); + } + if (t.getAnnotation(SqlMapper.class) == null) { + throw new BindingException("can not find SqlMapper annotation!"); + } + return recordsetUtil.getRsManager().commit(t.getName()); } /** @@ -2010,8 +2018,14 @@ public class Util extends weaver.general.Util { * @return boolean 是否回滚成功 * @author youHong.ai ****************************************** */ - public static boolean rollbackTransMapper() { - return recordsetUtil.getRsManager().rollback(); + public static boolean rollbackTransMapper(Class t) { + if (t == null) { + throw new NullPointerException("can not commit trans for null mapper proxy!"); + } + if (t.getAnnotation(SqlMapper.class) == null) { + throw new BindingException("can not find SqlMapper annotation!"); + } + return recordsetUtil.getRsManager().rollback(t.getName()); } /** diff --git a/src/main/java/aiyh/utils/recordset/RecordsetUtil.java b/src/main/java/aiyh/utils/recordset/RecordsetUtil.java index e82a60f..29145e3 100644 --- a/src/main/java/aiyh/utils/recordset/RecordsetUtil.java +++ b/src/main/java/aiyh/utils/recordset/RecordsetUtil.java @@ -54,10 +54,10 @@ public class RecordsetUtil implements InvocationHandler { private Object invokeRs(Object proxy, Method method, Object[] args) { - RecordSet rs = rsManager.getRs(); + RecordSet rs = rsManager.getRs(method.getDeclaringClass().getName()); if (rs == null) { - rsManager.setRecordSet(); - rs = rsManager.getRs(); + rsManager.setRecordSet(method.getDeclaringClass().getName()); + rs = rsManager.getRs(method.getDeclaringClass().getName()); } SqlHandler sqlHandler = new SqlHandler(); ResultMapper resultMapper = new ResultMapper(); @@ -230,10 +230,10 @@ public class RecordsetUtil implements InvocationHandler { } private Object invokeRsTrans(Object proxy, Method method, Object[] args) { - RecordSetTrans rs = rsManager.getTrans(); + RecordSetTrans rs = rsManager.getTrans(method.getDeclaringClass().getName()); if (rs == null) { - rsManager.setRecordSetTrans(); - rs = rsManager.getTrans(); + rsManager.setRecordSetTrans(method.getDeclaringClass().getName()); + rs = rsManager.getTrans(method.getDeclaringClass().getName()); } SqlHandler sqlHandler = new SqlHandler(); ResultMapper resultMapper = new ResultMapper(); diff --git a/src/main/java/aiyh/utils/recordset/RsThreadLocalManager.java b/src/main/java/aiyh/utils/recordset/RsThreadLocalManager.java index deacc1b..5950668 100644 --- a/src/main/java/aiyh/utils/recordset/RsThreadLocalManager.java +++ b/src/main/java/aiyh/utils/recordset/RsThreadLocalManager.java @@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit; public class RsThreadLocalManager { - private final ConcurrentHashMap rsMap = new ConcurrentHashMap<>(); + private final ConcurrentHashMap> rsMap = new ConcurrentHashMap<>(); private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); @@ -49,11 +49,19 @@ public class RsThreadLocalManager { * @author youHong.ai ****************************************** */ private void checkExpireRs() { - Iterator> iterator = rsMap.entrySet().iterator(); + Iterator>> iterator = rsMap.entrySet().iterator(); while (iterator.hasNext()) { - Map.Entry entity = iterator.next(); - RsThreadLocalMap value = entity.getValue(); - if (System.currentTimeMillis() >= value.getExpireTime() || value.getExpireTime() != -1) { + Map.Entry> entity = iterator.next(); + Map map = entity.getValue(); + Iterator> mapIterator = map.entrySet().iterator(); + while (mapIterator.hasNext()) { + Map.Entry mapEntity = mapIterator.next(); + RsThreadLocalMap value = mapEntity.getValue(); + if (System.currentTimeMillis() >= value.getExpireTime() || value.getExpireTime() != -1) { + mapIterator.remove(); + } + } + if (map.isEmpty()) { iterator.remove(); } } @@ -66,9 +74,18 @@ public class RsThreadLocalManager { * * @author youHong.ai ****************************************** */ - public void setRecordSet() { + public void setRecordSet(String className) { RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(new RecordSet(), getExpireTime()); - rsMap.put(Thread.currentThread().getId(), rsThreadLocalMap); + setRecordSetOrTrans(className, rsThreadLocalMap); + } + + private void setRecordSetOrTrans(String className, RsThreadLocalMap rsThreadLocalMap) { + Map map = rsMap.get(Thread.currentThread().getId()); + if (Objects.isNull(map)) { + map = new ConcurrentHashMap<>(); + } + map.put(className, rsThreadLocalMap); + rsMap.put(Thread.currentThread().getId(), map); } /** @@ -78,11 +95,11 @@ public class RsThreadLocalManager { * * @author youHong.ai ****************************************** */ - public void setRecordSetTrans() { + public void setRecordSetTrans(String className) { RecordSetTrans recordSetTrans = new RecordSetTrans(); recordSetTrans.setAutoCommit(false); RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(recordSetTrans, -1L); - rsMap.put(Thread.currentThread().getId(), rsThreadLocalMap); + setRecordSetOrTrans(className, rsThreadLocalMap); } /** @@ -106,8 +123,12 @@ public class RsThreadLocalManager { * @return RecordSet * @author youHong.ai ****************************************** */ - public RecordSet getRs() { - RsThreadLocalMap rsThreadLocalMap = rsMap.get(Thread.currentThread().getId()); + public RecordSet getRs(String className) { + Map map = rsMap.get(Thread.currentThread().getId()); + if (Objects.isNull(map)) { + return null; + } + RsThreadLocalMap rsThreadLocalMap = map.get(className); if (Objects.isNull(rsThreadLocalMap)) { return null; } @@ -123,8 +144,12 @@ public class RsThreadLocalManager { * @return RecordSetTrans 事务rs * @author youHong.ai ****************************************** */ - public RecordSetTrans getTrans() { - RsThreadLocalMap rsThreadLocalMap = rsMap.get(Thread.currentThread().getId()); + public RecordSetTrans getTrans(String className) { + Map map = rsMap.get(Thread.currentThread().getId()); + if (Objects.isNull(map)) { + return null; + } + RsThreadLocalMap rsThreadLocalMap = map.get(className); if (Objects.isNull(rsThreadLocalMap)) { return null; } @@ -132,24 +157,28 @@ public class RsThreadLocalManager { return rsThreadLocalMap.getRecordSetTrans(); } - public boolean commit() { - RsThreadLocalMap rsThreadLocalMap = rsMap.get(Thread.currentThread().getId()); - if (Objects.isNull(rsThreadLocalMap)) { - throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java!"); - } - rsThreadLocalMap.setExpireTime(getExpireTime()); - RecordSetTrans recordSetTrans = rsThreadLocalMap.getRecordSetTrans(); + public boolean commit(String className) { + RecordSetTrans recordSetTrans = getRecordSetTrans(className); return recordSetTrans.commit(); } - - public boolean rollback() { - RsThreadLocalMap rsThreadLocalMap = rsMap.get(Thread.currentThread().getId()); + private RecordSetTrans getRecordSetTrans(String className) { + Map map = rsMap.get(Thread.currentThread().getId()); + if (Objects.isNull(map)) { + throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java!"); + } + RsThreadLocalMap rsThreadLocalMap = map.get(className); if (Objects.isNull(rsThreadLocalMap)) { throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java!"); } rsThreadLocalMap.setExpireTime(getExpireTime()); RecordSetTrans recordSetTrans = rsThreadLocalMap.getRecordSetTrans(); + return recordSetTrans; + } + + + public boolean rollback(String className) { + RecordSetTrans recordSetTrans = getRecordSetTrans(className); return recordSetTrans.rollback(); } diff --git a/src/test/java/youhong/ai/pcn/UtilTest.java b/src/test/java/youhong/ai/pcn/UtilTest.java index 6ee36c1..516502e 100644 --- a/src/test/java/youhong/ai/pcn/UtilTest.java +++ b/src/test/java/youhong/ai/pcn/UtilTest.java @@ -22,12 +22,13 @@ public class UtilTest extends BaseTest { TransTestMapper transMapper = Util.getTransMapper(TransTestMapper.class); Student student = Builder.builder(Student::new) - .with(Student::setName, "王小明") + .with(Student::setName, "王小明2") .with(Student::setAge, 10) .with(Student::setSex, 1).build(); boolean b = transMapper.insertStudent(student); System.out.println(b); - System.out.println(Util.commitTransMapper()); + //boolean b1 = Util.rollbackTransMapper(TransTestMapper.class); + System.out.println(Util.commitTransMapper(TransTestMapper.class)); } }