Commit ca07ca28 by s_guodong

增加tbldbcounter

parent c70a2719
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.brilliance.mda.support.mybatis.count.mapper.TblCounterMapper">
<insert id="insertNewCounter" >
insert into TBLCNT(typ,VAL,STP) values(#{typ},#{start},#{stp})
</insert>
<select id="getCountValWithUpdate" resultType="java.lang.Integer">
select VAL from TBLCNT where TYP = #{typ} for update
</select>
<update id="updateCounter" >
update TBLCNT set VAL = VAL + STP where TYP = #{typ}
</update>
<select id="seqNextval" resultType="java.lang.Integer" >
select ${value}.nextval from dual
</select>
<select id="dbCounter" resultType="java.lang.Integer">
select f_dbcounter(#{seqName}) from dual;
</select>
</mapper>
\ No newline at end of file
...@@ -276,6 +276,11 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession ...@@ -276,6 +276,11 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
} }
@Override @Override
public int tblDbCounter(String seqname) {
return this.counter.tblDbCounter(seqname);
}
@Override
public void dbConnect() { public void dbConnect() {
sqlSessionTemplate.getSqlSessionFactory().openSession(false); sqlSessionTemplate.getSqlSessionFactory().openSession(false);
} }
......
...@@ -2,6 +2,7 @@ package com.brilliance.mda.support.mybatis.count; ...@@ -2,6 +2,7 @@ package com.brilliance.mda.support.mybatis.count;
import com.brilliance.mda.support.mybatis.count.mapper.CounterMapper; import com.brilliance.mda.support.mybatis.count.mapper.CounterMapper;
import com.brilliance.mda.support.mybatis.count.mapper.TblCounterMapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -19,10 +20,20 @@ public class CounterImplWithOracleTransaction implements CounterService { ...@@ -19,10 +20,20 @@ public class CounterImplWithOracleTransaction implements CounterService {
@Resource @Resource
public CounterMapper counterMapper; public CounterMapper counterMapper;
@Resource
public TblCounterMapper tblCounterMapper;
public int dbCounter(String seqname) { public int dbCounter(String seqname) {
String seq = "SEQ_" + seqname.toUpperCase(); String seq = "SEQ_" + seqname.toUpperCase();
return counterMapper.seqNextval(seq); return counterMapper.seqNextval(seq);
} }
@Override
public int tblDbCounter(String seqname) {
String seq = "SEQ_" + seqname.toUpperCase();
return tblCounterMapper.seqNextval(seq);
}
} }
...@@ -3,6 +3,7 @@ package com.brilliance.mda.support.mybatis.count; ...@@ -3,6 +3,7 @@ package com.brilliance.mda.support.mybatis.count;
import com.brilliance.mda.runtime.mda.RuleExecuteException; import com.brilliance.mda.runtime.mda.RuleExecuteException;
import com.brilliance.mda.support.mybatis.count.mapper.CounterMapper; import com.brilliance.mda.support.mybatis.count.mapper.CounterMapper;
import com.brilliance.mda.support.mybatis.count.mapper.TblCounterMapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -20,33 +21,55 @@ public class CounterImplWithSpringTransaction implements CounterService { ...@@ -20,33 +21,55 @@ public class CounterImplWithSpringTransaction implements CounterService {
@Resource @Resource
public CounterMapper counterMapper; public CounterMapper counterMapper;
@Resource
public TblCounterMapper tblCounterMapper;
public ReentrantLock lock = new ReentrantLock(); public ReentrantLock lock = new ReentrantLock();
@Transactional(propagation=Propagation.REQUIRES_NEW) @Transactional(propagation = Propagation.REQUIRES_NEW)
public int dbCounter(String seqname) public int dbCounter(String seqname) {
{
Integer cnt = counterMapper.getCountValWithUpdate(seqname); Integer cnt = counterMapper.getCountValWithUpdate(seqname);
if(cnt == null) cnt = 0; if (cnt == null) cnt = 0;
if(cnt == 0) if (cnt == 0) {
{
lock.lock(); lock.lock();
try { try {
cnt = counterMapper.getCountValWithUpdate(seqname); cnt = counterMapper.getCountValWithUpdate(seqname);
if(cnt == null) { if (cnt == null) {
cnt = 0; cnt = 0;
counterMapper.insertNewCounter(seqname, 1); //插入新数 counterMapper.insertNewCounter(seqname, 1); //插入新数
} } else
else
counterMapper.updateCounter(seqname); //计算器增加 counterMapper.updateCounter(seqname); //计算器增加
}catch (Exception e) } catch (Exception e) {
{ throw (new RuleExecuteException("主键生成异常", e));
throw(new RuleExecuteException("主键生成异常",e)); } finally {
}finally {
lock.unlock(); lock.unlock();
} }
} } else
else
counterMapper.updateCounter(seqname); //计算器增加 counterMapper.updateCounter(seqname); //计算器增加
return cnt; return cnt;
} }
@Transactional(propagation = Propagation.REQUIRES_NEW)
public int tblDbCounter(String seqname) {
Integer cnt = tblCounterMapper.getCountValWithUpdate(seqname);
if (cnt == null) cnt = 0;
if (cnt == 0) {
lock.lock();
try {
cnt = tblCounterMapper.getCountValWithUpdate(seqname);
if (cnt == null) {
cnt = 0;
tblCounterMapper.insertNewCounter(seqname, 1); //插入新数
} else
tblCounterMapper.updateCounter(seqname); //计算器增加
} catch (Exception e) {
throw (new RuleExecuteException("主键生成异常", e));
} finally {
lock.unlock();
}
} else
tblCounterMapper.updateCounter(seqname); //计算器增加
return cnt;
}
} }
...@@ -2,4 +2,6 @@ package com.brilliance.mda.support.mybatis.count; ...@@ -2,4 +2,6 @@ package com.brilliance.mda.support.mybatis.count;
public interface CounterService { public interface CounterService {
int dbCounter(String seqname); int dbCounter(String seqname);
int tblDbCounter(String seqname);
} }
package com.brilliance.mda.support.mybatis.count.mapper;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class TblCounterMapper {
final String FNAME = this.getClass().getName();
@Autowired
public SqlSessionTemplate template;
public Integer getCountValWithUpdate(@Param("typ") String seqName){
return template.selectOne(FNAME+".getCountValWithUpdate",seqName);
}
public int insertNewCounter(@Param("typ") String typ, @Param("stp") int stp){
Map<String, Object> map = new HashMap<>();
map.put("typ", typ);
map.put("start",1);
map.put("stp", stp);
return template.insert(FNAME+".insertNewCounter",map);
}
public void updateCounter(@Param("typ") String seqName){
template.selectOne(FNAME+".updateCounter",seqName);
}
public int seqNextval(String seqName) {
return template.selectOne(FNAME+".seqNextval",seqName);
}
public int dbCounter(String seqName){
return template.selectOne(FNAME+".dbCounter",seqName);
}
}
\ No newline at end of file
...@@ -51,6 +51,7 @@ public interface IDaoSession { ...@@ -51,6 +51,7 @@ public interface IDaoSession {
*/ */
int dbCounter(String seqname); int dbCounter(String seqname);
int tblDbCounter(String seqname);
void dbConnect(); void dbConnect();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment