同一个线程支持多个事务mapper

main
youHong.ai 2022-12-21 23:58:45 +08:00
parent 8e6720335d
commit f2b3189e4f
4 changed files with 79 additions and 35 deletions

View File

@ -2,7 +2,9 @@ package aiyh.utils;
import aiyh.utils.action.CusBaseAction; import aiyh.utils.action.CusBaseAction;
import aiyh.utils.annotation.*; import aiyh.utils.annotation.*;
import aiyh.utils.annotation.recordset.SqlMapper;
import aiyh.utils.entity.*; import aiyh.utils.entity.*;
import aiyh.utils.excention.BindingException;
import aiyh.utils.excention.CustomerException; import aiyh.utils.excention.CustomerException;
import aiyh.utils.fileUtil.ProperUtil; import aiyh.utils.fileUtil.ProperUtil;
import aiyh.utils.mapUtil.UtilHashMap; import aiyh.utils.mapUtil.UtilHashMap;
@ -1998,8 +2000,14 @@ public class Util extends weaver.general.Util {
* @return boolean * @return boolean
* @author youHong.ai ****************************************** * @author youHong.ai ******************************************
*/ */
public static boolean commitTransMapper() { public static <T> boolean commitTransMapper(Class<T> t) {
return recordsetUtil.getRsManager().commit(); 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 * @return boolean
* @author youHong.ai ****************************************** * @author youHong.ai ******************************************
*/ */
public static boolean rollbackTransMapper() { public static <T> boolean rollbackTransMapper(Class<T> t) {
return recordsetUtil.getRsManager().rollback(); 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());
} }
/** /**

View File

@ -54,10 +54,10 @@ public class RecordsetUtil implements InvocationHandler {
private Object invokeRs(Object proxy, Method method, Object[] args) { private Object invokeRs(Object proxy, Method method, Object[] args) {
RecordSet rs = rsManager.getRs(); RecordSet rs = rsManager.getRs(method.getDeclaringClass().getName());
if (rs == null) { if (rs == null) {
rsManager.setRecordSet(); rsManager.setRecordSet(method.getDeclaringClass().getName());
rs = rsManager.getRs(); rs = rsManager.getRs(method.getDeclaringClass().getName());
} }
SqlHandler sqlHandler = new SqlHandler(); SqlHandler sqlHandler = new SqlHandler();
ResultMapper resultMapper = new ResultMapper(); ResultMapper resultMapper = new ResultMapper();
@ -230,10 +230,10 @@ public class RecordsetUtil implements InvocationHandler {
} }
private Object invokeRsTrans(Object proxy, Method method, Object[] args) { private Object invokeRsTrans(Object proxy, Method method, Object[] args) {
RecordSetTrans rs = rsManager.getTrans(); RecordSetTrans rs = rsManager.getTrans(method.getDeclaringClass().getName());
if (rs == null) { if (rs == null) {
rsManager.setRecordSetTrans(); rsManager.setRecordSetTrans(method.getDeclaringClass().getName());
rs = rsManager.getTrans(); rs = rsManager.getTrans(method.getDeclaringClass().getName());
} }
SqlHandler sqlHandler = new SqlHandler(); SqlHandler sqlHandler = new SqlHandler();
ResultMapper resultMapper = new ResultMapper(); ResultMapper resultMapper = new ResultMapper();

View File

@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit;
public class RsThreadLocalManager { public class RsThreadLocalManager {
private final ConcurrentHashMap<Long, RsThreadLocalMap> rsMap = new ConcurrentHashMap<>(); private final ConcurrentHashMap<Long, Map<String, RsThreadLocalMap>> rsMap = new ConcurrentHashMap<>();
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
@ -49,11 +49,19 @@ public class RsThreadLocalManager {
* @author youHong.ai ****************************************** * @author youHong.ai ******************************************
*/ */
private void checkExpireRs() { private void checkExpireRs() {
Iterator<Map.Entry<Long, RsThreadLocalMap>> iterator = rsMap.entrySet().iterator(); Iterator<Map.Entry<Long, Map<String, RsThreadLocalMap>>> iterator = rsMap.entrySet().iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Map.Entry<Long, RsThreadLocalMap> entity = iterator.next(); Map.Entry<Long, Map<String, RsThreadLocalMap>> entity = iterator.next();
RsThreadLocalMap value = entity.getValue(); Map<String, RsThreadLocalMap> map = entity.getValue();
if (System.currentTimeMillis() >= value.getExpireTime() || value.getExpireTime() != -1) { Iterator<Map.Entry<String, RsThreadLocalMap>> mapIterator = map.entrySet().iterator();
while (mapIterator.hasNext()) {
Map.Entry<String, RsThreadLocalMap> mapEntity = mapIterator.next();
RsThreadLocalMap value = mapEntity.getValue();
if (System.currentTimeMillis() >= value.getExpireTime() || value.getExpireTime() != -1) {
mapIterator.remove();
}
}
if (map.isEmpty()) {
iterator.remove(); iterator.remove();
} }
} }
@ -66,9 +74,18 @@ public class RsThreadLocalManager {
* *
* @author youHong.ai ****************************************** * @author youHong.ai ******************************************
*/ */
public void setRecordSet() { public void setRecordSet(String className) {
RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(new RecordSet(), getExpireTime()); RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(new RecordSet(), getExpireTime());
rsMap.put(Thread.currentThread().getId(), rsThreadLocalMap); setRecordSetOrTrans(className, rsThreadLocalMap);
}
private void setRecordSetOrTrans(String className, RsThreadLocalMap rsThreadLocalMap) {
Map<String, RsThreadLocalMap> 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 ****************************************** * @author youHong.ai ******************************************
*/ */
public void setRecordSetTrans() { public void setRecordSetTrans(String className) {
RecordSetTrans recordSetTrans = new RecordSetTrans(); RecordSetTrans recordSetTrans = new RecordSetTrans();
recordSetTrans.setAutoCommit(false); recordSetTrans.setAutoCommit(false);
RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(recordSetTrans, -1L); RsThreadLocalMap rsThreadLocalMap = new RsThreadLocalMap(recordSetTrans, -1L);
rsMap.put(Thread.currentThread().getId(), rsThreadLocalMap); setRecordSetOrTrans(className, rsThreadLocalMap);
} }
/** /**
@ -106,8 +123,12 @@ public class RsThreadLocalManager {
* @return RecordSet * @return RecordSet
* @author youHong.ai ****************************************** * @author youHong.ai ******************************************
*/ */
public RecordSet getRs() { public RecordSet getRs(String className) {
RsThreadLocalMap rsThreadLocalMap = rsMap.get(Thread.currentThread().getId()); Map<String, RsThreadLocalMap> map = rsMap.get(Thread.currentThread().getId());
if (Objects.isNull(map)) {
return null;
}
RsThreadLocalMap rsThreadLocalMap = map.get(className);
if (Objects.isNull(rsThreadLocalMap)) { if (Objects.isNull(rsThreadLocalMap)) {
return null; return null;
} }
@ -123,8 +144,12 @@ public class RsThreadLocalManager {
* @return RecordSetTrans rs * @return RecordSetTrans rs
* @author youHong.ai ****************************************** * @author youHong.ai ******************************************
*/ */
public RecordSetTrans getTrans() { public RecordSetTrans getTrans(String className) {
RsThreadLocalMap rsThreadLocalMap = rsMap.get(Thread.currentThread().getId()); Map<String, RsThreadLocalMap> map = rsMap.get(Thread.currentThread().getId());
if (Objects.isNull(map)) {
return null;
}
RsThreadLocalMap rsThreadLocalMap = map.get(className);
if (Objects.isNull(rsThreadLocalMap)) { if (Objects.isNull(rsThreadLocalMap)) {
return null; return null;
} }
@ -132,24 +157,28 @@ public class RsThreadLocalManager {
return rsThreadLocalMap.getRecordSetTrans(); return rsThreadLocalMap.getRecordSetTrans();
} }
public boolean commit() { public boolean commit(String className) {
RsThreadLocalMap rsThreadLocalMap = rsMap.get(Thread.currentThread().getId()); RecordSetTrans recordSetTrans = getRecordSetTrans(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.commit(); return recordSetTrans.commit();
} }
private RecordSetTrans getRecordSetTrans(String className) {
public boolean rollback() { Map<String, RsThreadLocalMap> map = rsMap.get(Thread.currentThread().getId());
RsThreadLocalMap rsThreadLocalMap = 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)) { if (Objects.isNull(rsThreadLocalMap)) {
throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java"); throw new CustomerException("can not find RecordSetTrans instance! please Contact the developer of RecordsetUtil.java");
} }
rsThreadLocalMap.setExpireTime(getExpireTime()); rsThreadLocalMap.setExpireTime(getExpireTime());
RecordSetTrans recordSetTrans = rsThreadLocalMap.getRecordSetTrans(); RecordSetTrans recordSetTrans = rsThreadLocalMap.getRecordSetTrans();
return recordSetTrans;
}
public boolean rollback(String className) {
RecordSetTrans recordSetTrans = getRecordSetTrans(className);
return recordSetTrans.rollback(); return recordSetTrans.rollback();
} }

View File

@ -22,12 +22,13 @@ public class UtilTest extends BaseTest {
TransTestMapper transMapper = Util.getTransMapper(TransTestMapper.class); TransTestMapper transMapper = Util.getTransMapper(TransTestMapper.class);
Student student = Builder.builder(Student::new) Student student = Builder.builder(Student::new)
.with(Student::setName, "王小明") .with(Student::setName, "王小明2")
.with(Student::setAge, 10) .with(Student::setAge, 10)
.with(Student::setSex, 1).build(); .with(Student::setSex, 1).build();
boolean b = transMapper.insertStudent(student); boolean b = transMapper.insertStudent(student);
System.out.println(b); System.out.println(b);
System.out.println(Util.commitTransMapper()); //boolean b1 = Util.rollbackTransMapper(TransTestMapper.class);
System.out.println(Util.commitTransMapper(TransTestMapper.class));
} }
} }