diff --git a/src/main/java/com/sunyard/sge/database/SydApiBaseFunction.java b/src/main/java/com/sunyard/sge/database/SydApiBaseFunction.java index 26b23e2..05e58e8 100644 --- a/src/main/java/com/sunyard/sge/database/SydApiBaseFunction.java +++ b/src/main/java/com/sunyard/sge/database/SydApiBaseFunction.java @@ -166,6 +166,9 @@ public class SydApiBaseFunction implements SydApi { } } } + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } @@ -230,7 +233,9 @@ public class SydApiBaseFunction implements SydApi { } } } - + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } @@ -304,6 +309,9 @@ public class SydApiBaseFunction implements SydApi { } } } + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } @@ -371,6 +379,9 @@ public class SydApiBaseFunction implements SydApi { } } } + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } @@ -453,7 +464,9 @@ public class SydApiBaseFunction implements SydApi { } } } - + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } @@ -521,6 +534,9 @@ public class SydApiBaseFunction implements SydApi { } } } + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } @@ -589,6 +605,9 @@ public class SydApiBaseFunction implements SydApi { } } } + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } @@ -676,6 +695,9 @@ public class SydApiBaseFunction implements SydApi { } } } + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } @@ -746,6 +768,9 @@ public class SydApiBaseFunction implements SydApi { } } } + if ( ! this.isShortLinkMode ) { + this.oip.setError(); + } throw le; } diff --git a/src/main/java/com/sunyard/sge/database/pool/SydApiPool.java b/src/main/java/com/sunyard/sge/database/pool/SydApiPool.java index 909e695..de19a10 100644 --- a/src/main/java/com/sunyard/sge/database/pool/SydApiPool.java +++ b/src/main/java/com/sunyard/sge/database/pool/SydApiPool.java @@ -11,11 +11,12 @@ import org.apache.logging.log4j.Logger; import racal.sunyard.main.SydApi4j; public class SydApiPool extends ThreadBasedConnectPool { - private HsmLinkInfo[] linkInfos; static { LogFactory.createLogger("./logs", 0); } + + public SydApiPool(HsmLinkInfo[] linkInfos) { super(); this.linkInfos = linkInfos; @@ -23,7 +24,7 @@ public class SydApiPool extends ThreadBasedConnectPool { // 继承 初始化 protected SydApi initialValue() { - + Logger log = LogFactory.getLogger(); // 所有加密机一次性链接 SydApi4j[] apis = new SydApi4j[linkInfos.length]; @@ -40,15 +41,12 @@ public class SydApiPool extends ThreadBasedConnectPool { ); connected = true; } catch (Exception e) { - Logger log = LogFactory.getLogger(); // 忽略单个错误 log.error("网络连接错误 {} {} - 忽略", info.getPcIp(), info.getiPort()); } } - if (!connected) { - throw new SydApiException("连接错误", -1); - } + if (!connected) { throw new SydApiException("连接错误", -1); } return new SydApiBaseFunction(apis, linkInfos); @@ -56,7 +54,7 @@ public class SydApiPool extends ThreadBasedConnectPool { // 保活接口 public boolean check(SydApi s) { - if( !(s instanceof SydApi4Database) ) { + if (!(s instanceof SydApi4Database)) { return false; } @@ -84,7 +82,7 @@ public class SydApiPool extends ThreadBasedConnectPool { try { hsms[i] = (SydApi4j) new SydApi4j().connect(info.getPcIp(), info.getiPort(), null, info.getiConnectTimeOut(), info.getiDealTimeOut()); canUse = true; - } catch ( Exception e ) { + } catch (Exception e) { } } @@ -99,19 +97,19 @@ public class SydApiPool extends ThreadBasedConnectPool { // 释放 protected void free(SydApi o) { if (null != o) { - if (o instanceof SydApi4Database) { - ((SydApi4Database) o).SYD_Disconnect(); + if (o instanceof SydApiBaseFunction) { + ((SydApiBaseFunction) o).SYD_Disconnect(); } } } - protected void afterInit(ObjectInPool oip, SydApi value ){ - if ( !( value instanceof SydApi4Database ) ) { + protected void afterInit(ObjectInPool oip, SydApi value) { + if (!(value instanceof SydApiBaseFunction)) { return; } - SydApi4Database api = (SydApi4Database) value; - api.setOip( oip ); + SydApiBaseFunction api = (SydApiBaseFunction) value; + api.setOip(oip); } } diff --git a/src/main/java/com/sunyard/sge/pool/ObjectInPool.java b/src/main/java/com/sunyard/sge/pool/ObjectInPool.java index 944a4cb..b80558d 100644 --- a/src/main/java/com/sunyard/sge/pool/ObjectInPool.java +++ b/src/main/java/com/sunyard/sge/pool/ObjectInPool.java @@ -4,9 +4,14 @@ public class ObjectInPool { private T object; private long lasUseAt = System.currentTimeMillis(); + private ThreadBasedConnectPool pool; + private Object key; - public ObjectInPool(T object) { + + public ObjectInPool(ThreadBasedConnectPool pool, Object key, T object) { this.object = object; + this.pool = pool; + this.key = key; } public T getObjectForCheck() { @@ -25,4 +30,9 @@ public class ObjectInPool { public long getLasUseAt() { return lasUseAt; } + + public void setError(){ + this.pool.remove( this.key ); + } + } diff --git a/src/main/java/com/sunyard/sge/pool/ThreadBasedConnectPool.java b/src/main/java/com/sunyard/sge/pool/ThreadBasedConnectPool.java index 60aace6..f323819 100644 --- a/src/main/java/com/sunyard/sge/pool/ThreadBasedConnectPool.java +++ b/src/main/java/com/sunyard/sge/pool/ThreadBasedConnectPool.java @@ -1,6 +1,8 @@ package com.sunyard.sge.pool; -import com.sunyard.sge.database.SydApi; + + + import com.sunyard.sge.log.LogFactory; import org.apache.logging.log4j.Logger; @@ -12,9 +14,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ThreadBasedConnectPool { - static { - LogFactory.createLogger("./logs", 0); - } + // ThreadLocal private final Map> map = new HashMap<>(); @@ -23,13 +23,16 @@ public class ThreadBasedConnectPool { // 配置 private PoolConfig config; + static { + LogFactory.createLogger("./logs", 0); + } public ThreadBasedConnectPool(PoolConfig config) { this.config = config; + Logger log = LogFactory.getLogger(); threadPool.scheduleAtFixedRate(new Runnable() { @Override public void run() { - Logger log = LogFactory.getLogger(); log.debug("连接池检测进程开始执行"); log.debug("共有链接 {} 个", map.size()); @@ -129,7 +132,7 @@ public class ThreadBasedConnectPool { // 设置 public void set(T o) { - map.put(Thread.currentThread(), new ObjectInPool<>(o)); + map.put(Thread.currentThread(), new ObjectInPool<>(this, Thread.currentThread(), o)); } // 清除 @@ -137,13 +140,17 @@ public class ThreadBasedConnectPool { map.remove(Thread.currentThread()); } + public void remove(Object key) { + map.remove(key); + } + protected void afterInit(ObjectInPool oip, T value) { } private T setInitialValue() { T value = initialValue(); - ObjectInPool opi = new ObjectInPool<>(value); + ObjectInPool opi = new ObjectInPool<>(this, Thread.currentThread(), value); map.put(Thread.currentThread(), opi); afterInit(opi, value); return value;