|
|
@@ -18,8 +18,12 @@ public class JdbcUtils {
|
|
|
public static String Drivde="org.sqlite.JDBC";
|
|
|
public static String DATABASE_URL="jdbc:sqlite:mes_db.db";
|
|
|
|
|
|
- public static Connection getConn(){
|
|
|
+ // 全局复用一个连接,已打开则直接返回,避免每次新建导致连接泄漏
|
|
|
+ public static synchronized Connection getConn(){
|
|
|
try {
|
|
|
+ if(conn != null && !conn.isClosed()){
|
|
|
+ return conn;
|
|
|
+ }
|
|
|
Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
|
|
|
conn = DriverManager.getConnection(DATABASE_URL);//连接数据库zhou.db,不存在则创建
|
|
|
create_bw_record();//初始化结构表
|
|
|
@@ -98,22 +102,16 @@ public class JdbcUtils {
|
|
|
statement.close();
|
|
|
}
|
|
|
|
|
|
- public static CacheData getCache(String key) {
|
|
|
- //SQL语句
|
|
|
- String sql = "select id,sn,content from cache where sn = '"+key+"' order by id asc limit 1";
|
|
|
- Connection conn = JdbcUtils.getConn();
|
|
|
- Statement stmt=null;
|
|
|
- ResultSet ret = null;
|
|
|
+ public static synchronized CacheData getCache(String key) {
|
|
|
String content = "";
|
|
|
- try {
|
|
|
- stmt = conn.createStatement();
|
|
|
- //执行语句,得到结果集
|
|
|
- ret = stmt.executeQuery(sql);
|
|
|
- while (ret.next()) {
|
|
|
- content = ret.getString(3);
|
|
|
+ // 复用全局连接,不再关闭,避免关掉其他线程正在用的连接
|
|
|
+ try (PreparedStatement ps = getConn().prepareStatement("select content from cache where sn = ? order by id asc limit 1")) {
|
|
|
+ ps.setString(1, key);
|
|
|
+ try (ResultSet ret = ps.executeQuery()) {
|
|
|
+ if (ret.next()) {
|
|
|
+ content = ret.getString(1);
|
|
|
+ }
|
|
|
}
|
|
|
- ret.close();
|
|
|
- conn.close();//关闭连接
|
|
|
} catch (SQLException e1) {
|
|
|
e1.printStackTrace();
|
|
|
}
|
|
|
@@ -124,40 +122,28 @@ public class JdbcUtils {
|
|
|
return JSON.parseObject(content,CacheData.class);
|
|
|
}
|
|
|
|
|
|
- public static Boolean setCache(String key,String value) {
|
|
|
- String record_time = DateLocalUtils.getCurrentTime();
|
|
|
- Connection conn = JdbcUtils.getConn();
|
|
|
+ public static synchronized Boolean setCache(String key,String value) {
|
|
|
if(value == null || value.trim().isEmpty()) {
|
|
|
return false;
|
|
|
}
|
|
|
- value = CommonUtils.base64Encode(value);
|
|
|
- CacheData content = getCache(key);
|
|
|
- if(content == null) { // 新增
|
|
|
- try {
|
|
|
- Statement statement=conn.createStatement();
|
|
|
- statement.executeUpdate("INSERT INTO cache (sn,content,record_time) VALUES"
|
|
|
- + " ('"+key+"', '"+value+"','"+record_time+"')");//向数据库中插入数据
|
|
|
- statement.close();
|
|
|
- }catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }else{
|
|
|
- try{
|
|
|
- Statement statement=conn.createStatement(); //创建连接对象,是Java的一个操作数据库的重要接口
|
|
|
- statement.executeUpdate("update cache set content = '"+value.trim() + "' where sn = '"+key+"'");
|
|
|
- statement.close();
|
|
|
- }catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ value = CommonUtils.base64Encode(value).trim();
|
|
|
+ // sn 有唯一索引,存在则覆盖,不存在则新增,避免并发 insert 冲突
|
|
|
+ try (PreparedStatement ps = getConn().prepareStatement("INSERT OR REPLACE INTO cache (sn,content,record_time) VALUES (?,?,?)")) {
|
|
|
+ ps.setString(1, key);
|
|
|
+ ps.setString(2, value);
|
|
|
+ ps.setString(3, DateLocalUtils.getCurrentTime());
|
|
|
+ ps.executeUpdate();
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
//插入数据
|
|
|
- public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
|
|
|
+ public static synchronized boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
|
|
|
boolean ret = false;
|
|
|
String record_time = DateLocalUtils.getCurrentTime();
|
|
|
- if(conn==null) {
|
|
|
+ if(getConn()==null) {
|
|
|
ret = false;
|
|
|
}else {
|
|
|
try {
|
|
|
@@ -178,10 +164,10 @@ public class JdbcUtils {
|
|
|
}
|
|
|
|
|
|
// 向 submit_record表 插入提交记录数据
|
|
|
- public static boolean insertSubmitRecord(String oprno, String sn, String bw){
|
|
|
+ public static synchronized boolean insertSubmitRecord(String oprno, String sn, String bw){
|
|
|
boolean ret = false;
|
|
|
String record_time = DateLocalUtils.getCurrentTime();
|
|
|
- if(conn==null) {
|
|
|
+ if(getConn()==null) {
|
|
|
ret = false;
|
|
|
}else {
|
|
|
try {
|
|
|
@@ -228,7 +214,7 @@ public class JdbcUtils {
|
|
|
statement.close();
|
|
|
}
|
|
|
|
|
|
- public static void close(){
|
|
|
+ public static synchronized void close(){
|
|
|
log.info("SQLite数据库连接关闭!");
|
|
|
try {
|
|
|
if(conn!=null) {
|