@ -22,20 +22,30 @@ import com.fr.decision.webservice.bean.datasource.JDBCConnectionBean;
import com.fr.decision.webservice.utils.DecisionServiceConstants ;
import com.fr.decision.webservice.v10.datasource.connection.processor.impl.ConnectionProcessorFactory ;
import com.fr.decision.webservice.v10.datasource.connection.processor.impl.JDBCConnectionProcessor ;
import com.fr.event.Event ;
import com.fr.event.EventDispatcher ;
import com.fr.event.Listener ;
import com.fr.event.Null ;
import com.fr.log.FineLoggerFactory ;
import com.fr.module.tool.ActivatorToolBox ;
import com.fr.security.encryption.transmission.TransmissionEncryptionManager ;
import com.fr.stable.Constants ;
import com.fr.stable.StringUtils ;
import com.fr.third.fasterxml.jackson.databind.ObjectMapper ;
import com.fr.third.guava.cache.CacheBuilder ;
import com.fr.third.guava.cache.CacheLoader ;
import com.fr.third.guava.cache.LoadingCache ;
import com.fr.third.springframework.beans.BeanUtils ;
import com.fr.workspace.server.repository.WorkplaceConstants ;
import com.fr.workspace.server.repository.connection.ConnectionCacheEvent ;
import com.fr.workspace.server.repository.connection.ConnectionRepository ;
import com.fr.workspace.server.repository.tabledata.DataEncryptionHelper ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Optional ;
import java.util.concurrent.ExecutionException ;
import java.util.concurrent.TimeUnit ;
/ * *
* 数据连接传输工具类
@ -50,19 +60,38 @@ public class ConnectionInfoBeanHelper {
private static final int ORACLE_DEFAULT_FETCH_SIZE = 128 ;
private static final int DB2_DEFAULT_FETCH_SIZE = 50 ;
private static final int POSTGRE_DEFAULT_FETCH_SIZE = 10000 ;
public static final int CACHE_MAX_SIZE = 1 ;
public static final String VALUE = "value" ;
private static LoadingCache < String , Map < String , Connection > > cache = CacheBuilder . newBuilder ( )
// 缓存容量1
. maximumSize ( CACHE_MAX_SIZE )
// 默认写入过60S后过期
. expireAfterWrite ( 60 , TimeUnit . SECONDS )
. build ( new CacheLoader < String , Map < String , Connection > > ( ) {
@Override
public Map < String , Connection > load ( String s ) throws Exception {
return updateCache ( ) ;
}
} ) ;
static {
FETCH_SIZE_MAP . put ( "oracle" , ORACLE_DEFAULT_FETCH_SIZE ) ;
FETCH_SIZE_MAP . put ( "ibm-db2" , DB2_DEFAULT_FETCH_SIZE ) ;
FETCH_SIZE_MAP . put ( "postgresql" , POSTGRE_DEFAULT_FETCH_SIZE ) ;
// 监听数据连接缓存失效
EventDispatcher . listen ( ConnectionCacheEvent . REMOVE , new Listener < Null > ( ) {
@Override
public void on ( Event event , Null aNull ) {
cache . invalidateAll ( ) ;
}
} ) ;
}
/ * *
* 获取连接Map
* /
public static Map < String , Connection > createConnectionMap ( ConnectionInfoBean [ ] beans ) {
private static Map < String , Connection > updateCache ( ) {
Map < String , Connection > ans = new HashMap < > ( ) ;
ConnectionInfoBean [ ] beans = ConnectionRepository . getInstance ( ) . getAll ( ) ;
for ( ConnectionInfoBean infoBean : beans ) {
try {
if ( JDBCConnectionProcessor . KEY . acceptConnectionTypes ( ) . contains ( infoBean . getConnectionType ( ) ) ) {
@ -77,23 +106,33 @@ public class ConnectionInfoBeanHelper {
return ans ;
}
/ * *
* 获取连接Map
* /
public static Map < String , Connection > getCurrentConnectionMap ( ) {
try {
return cache . get ( VALUE ) ;
} catch ( ExecutionException e ) {
throw new RuntimeException ( e ) ;
}
}
/ * *
* 根据bean创建连接
* /
public static Connection createConnection ( ConnectionInfoBean infoBean ) {
Connection connection = null ;
try {
if ( JDBCConnectionProcessor . KEY . acceptConnectionTypes ( ) . contains ( infoBean . getConnectionType ( ) ) ) {
connection = createJDBCConnection ( infoBean ) ;
} else {
connection = ConnectionProcessorFactory . createConnection ( infoBean , true ) ;
}
connection . setConnectionName ( infoBean . getConnectionName ( ) ) ;
} catch ( Exception e ) {
FineLoggerFactory . getLogger ( ) . error ( e . getMessage ( ) , e ) ;
return connection ;
}
return connection ;
try {
Connection connection = cache . get ( VALUE ) . get ( infoBean . getConnectionName ( ) ) ;
if ( connection ! = null ) {
connection . setConnectionName ( infoBean . getConnectionName ( ) ) ;
}
return connection ;
} catch ( ExecutionException e ) {
throw new RuntimeException ( e ) ;
}
}
/ * *