Commit 05ced278 by WeiCong

增加拨号(cnt表使用)缓存器使用

parent 739cf430
package org.sss.module.pojo;
import log.Log;
import log.LogFactory;
import org.apache.commons.dbutils.DbUtils;
import org.hibernate.engine.spi.SessionImplementor;
import org.sss.module.hibernate.HibernateUtils;
import org.sss.presentation.noui.util.NoUiUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class CacheCounter {
static final Log log = LogFactory.getLog(CacheCounter.class);
private static final Map<String, Counter> CACHE = Collections.synchronizedMap(new HashMap());
private static final CacheCounter INSTANCE = new CacheCounter();
private static final String INSERTCNT = "INSERT INTO cnt (nam,val,stp) VALUES (?,?,?)";
private static final String UPDATECNT = "UPDATE cnt SET val=? WHERE nam= ?";
public static int count(String counterName) {
Map<String, Counter> cache = CACHE;
Counter count = cache.get(counterName);
if (count == null) {
synchronized (CacheCounter.class) {
count = cache.get(counterName);
if (count == null) {
count = INSTANCE.new Counter(counterName);
cache.put(counterName, count);
}
}
}
return count.getCurCount();
}
public class Counter {
AtomicInteger curCount = new AtomicInteger();
volatile int stopCount;
String counterName;
Object lock = new Object();
public Counter(String counterName) {
this.counterName = counterName;
loadCnt();
}
boolean needUpdate(int cur) {
return (cur >= stopCount) && (cur != 0);
}
public int getCurCount() {
int cur = curCount.getAndIncrement();
if (needUpdate(cur)) {
synchronized (lock) {
cur = curCount.getAndIncrement();
if (needUpdate(cur)) {
loadCnt();
cur = curCount.getAndIncrement();
}
}
}
return cur;
}
private int loadCnt() {
int countValue = 0;
int endCount=0;
SessionImplementor session = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
session = (SessionImplementor) HibernateUtils.openSession(null);
conn = session.connection();
if (conn.getAutoCommit()) {
conn.setAutoCommit(false);
}
String sql = DatabaseUtils.getCounterSelectSQL(conn, counterName);
boolean updatable = sql.endsWith("UPDATE");
stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, updatable ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery();
if (rs.next()) {
countValue = rs.getInt(1);
endCount = countValue + NoUiUtils.STP;
if (updatable) {
rs.updateInt(1, endCount);
rs.updateRow();
} else {
DbUtils.closeQuietly((Connection) null, stmt, rs);
stmt = conn.prepareStatement(UPDATECNT);
stmt.setInt(1, endCount);
stmt.setString(2, counterName);
stmt.execute();
}
} else {
countValue = 0;
endCount = NoUiUtils.STP;
DbUtils.closeQuietly((Connection) null, stmt, rs);
stmt = conn.prepareStatement(INSERTCNT);
stmt.setString(1, counterName);
stmt.setInt(2, endCount);
stmt.setInt(3, 1);
stmt.execute();
}
conn.commit();
} catch (Exception e) {
log.error("loadCnt error.", e);
} finally {
DbUtils.closeQuietly((Connection) null, stmt, rs);
curCount.set(countValue);
stopCount=endCount;
}
return countValue;
}
}
}
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