同一个线程支持多个事务mapper
parent
8e6720335d
commit
f2b3189e4f
|
@ -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 <T> boolean commitTransMapper(Class<T> 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 <T> boolean rollbackTransMapper(Class<T> 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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
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();
|
||||
|
||||
|
@ -49,11 +49,19 @@ public class RsThreadLocalManager {
|
|||
* @author youHong.ai ******************************************
|
||||
*/
|
||||
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()) {
|
||||
Map.Entry<Long, RsThreadLocalMap> entity = iterator.next();
|
||||
RsThreadLocalMap value = entity.getValue();
|
||||
if (System.currentTimeMillis() >= value.getExpireTime() || value.getExpireTime() != -1) {
|
||||
Map.Entry<Long, Map<String, RsThreadLocalMap>> entity = iterator.next();
|
||||
Map<String, RsThreadLocalMap> map = entity.getValue();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -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<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 ******************************************
|
||||
*/
|
||||
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<String, RsThreadLocalMap> 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<String, RsThreadLocalMap> 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<String, RsThreadLocalMap> 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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue