diff --git a/caf-data/caf-data-jdbc/src/main/java/io/iec/edp/caf/database/DatabaseManager.java b/caf-data/caf-data-jdbc/src/main/java/io/iec/edp/caf/database/DatabaseManager.java index da5f068cb26bc8904c3916c9adba212720dfcd59..29d87f267cf239f5993eedc5e3136818231c7897 100644 --- a/caf-data/caf-data-jdbc/src/main/java/io/iec/edp/caf/database/DatabaseManager.java +++ b/caf-data/caf-data-jdbc/src/main/java/io/iec/edp/caf/database/DatabaseManager.java @@ -34,7 +34,6 @@ import java.util.concurrent.ConcurrentHashMap; * @author guowenchang * @date 2020-11-03 */ -@Deprecated @Slf4j public class DatabaseManager { @@ -77,30 +76,26 @@ public class DatabaseManager { public void begin() throws SQLException { ensureInitialized(); + String databaseId = identifierResolver.resolveDataSourceIdentifier(); + Database database = getCurrentDatabase(databaseId); - synchronized (databaseThreadLocal) { - String databaseId = identifierResolver.resolveDataSourceIdentifier(); - Database database = getCurrentDatabase(databaseId); - - //上下文中不存在 则组建新的Database对象 - if (database == null) { - Connection connection = connectionProvider.getConnection(databaseId); - if(log.isDebugEnabled()) { - log.debug("\nthread ["+Thread.currentThread().getId()+"] is invoking [begin] method,context has no [Database],create new [Database]\n"); - } - database = new Database(connection,databaseId); - setCurrentDatabase(databaseId, database); - + //上下文中不存在 则组建新的Database对象 + if (database == null) { + Connection connection = connectionProvider.getConnection(databaseId); + if(log.isDebugEnabled()) { + log.debug("\nthread ["+Thread.currentThread().getId()+"] is invoking [begin] method,context has no [Database],create new [Database]\n"); } + database = new Database(connection,databaseId); + setCurrentDatabase(databaseId, database); + } - if(log.isDebugEnabled()){ - Throwable ex = new Throwable(); - log.debug("\nthread ["+Thread.currentThread().getId()+"]is invoking [begin] method,部分堆栈信息:\n",ex); - log.debug("\nthread ["+Thread.currentThread().getId()+"] current databaseId ["+databaseId+"] has connect to jdbc"+ - "\nthread ["+Thread.currentThread().getId()+"] current connection info:["+database.getJdbcTemplate().getDataSource().getConnection().toString()+"]"); - } + if(log.isDebugEnabled()){ + Throwable ex = new Throwable(); + log.debug("\nthread ["+Thread.currentThread().getId()+"]is invoking [begin] method,部分堆栈信息:\n",ex); + log.debug("\nthread ["+Thread.currentThread().getId()+"] current databaseId ["+databaseId+"] has connect to jdbc"+ + "\nthread ["+Thread.currentThread().getId()+"] current connection info:["+database.getJdbcTemplate().getDataSource().getConnection().toString()+"]"); } } @@ -117,30 +112,25 @@ public class DatabaseManager { */ public void end() { ensureInitialized(); + String databaseId = identifierResolver.resolveDataSourceIdentifier(); + Database database = getCurrentDatabase(databaseId); - synchronized (databaseThreadLocal) { - String databaseId = identifierResolver.resolveDataSourceIdentifier(); - Database database = getCurrentDatabase(databaseId); + if(log.isDebugEnabled()){ + Throwable ex = new Throwable(); + log.debug("\nthread ["+Thread.currentThread().getId()+"] is invoking [end] method,current databaseId ["+databaseId+"] ,stacktrace: "+ex); + } + //上下文中不存在 则组建新的Database对象 + if (database == null) { - if(log.isDebugEnabled()){ - Throwable ex = new Throwable(); - log.debug("\nthread ["+Thread.currentThread().getId()+"] is invoking [end] method,stacktrace: "+ex); + if(log.isDebugEnabled()) { + log.debug("\nthread ["+Thread.currentThread().getId()+"] is invoking [end] method: Database context is already closed, databaseId:"+databaseId); } - //上下文中不存在 则组建新的Database对象 - if (database == null) { - - if(log.isDebugEnabled()) { - log.debug("\nthread ["+Thread.currentThread().getId()+"] is invoking [end] method: Database context is already closed, databaseId:"+databaseId); - } - throw new RuntimeException("Database context is already closed."); - }else{ - database.release(); - //database.evictConnection(); - //database.destroyConnection(); - } - - clearCurrentDatabase(databaseId); + throw new RuntimeException("Database context is already closed."); + }else{ + database.release(); } + + clearCurrentDatabase(databaseId); } /** @@ -154,13 +144,6 @@ public class DatabaseManager { Database database = getCurrentDatabase(databaseId); return database != null; -// 去除同步锁,提高性能 -// synchronized (databaseThreadLocal) { -// String databaseId = identifierResolver.resolveDataSourceIdentifier(); -// Database database = getCurrentDatabase(databaseId); -// -// return database != null; -// } } /** @@ -171,18 +154,17 @@ public class DatabaseManager { */ public Database getDatabase() throws SQLException { ensureInitialized(); - - synchronized (databaseThreadLocal) { - String databaseId = identifierResolver.resolveDataSourceIdentifier(); - Database database = getCurrentDatabase(databaseId); + String databaseId = identifierResolver.resolveDataSourceIdentifier(); + Database database = getCurrentDatabase(databaseId); // //上下文中存在 则返回当前的 否则创建新的 // if (database == null) { // Connection connection = connectionProvider.getConnection(databaseId); // database = new Database(connection); // } - log.info("\nthread ["+Thread.currentThread().getId()+"] get [Database],databaseId:"+databaseId); - return database; + if(log.isDebugEnabled()){ + log.debug("\nthread ["+Thread.currentThread().getId()+"] get [Database],databaseId:"+databaseId); } + return database; } /** @@ -194,16 +176,13 @@ public class DatabaseManager { */ public void requiteDatabase(Database db) { ensureInitialized(); + String databaseId = identifierResolver.resolveDataSourceIdentifier(); + Database database = getCurrentDatabase(databaseId); - synchronized (databaseThreadLocal) { - String databaseId = identifierResolver.resolveDataSourceIdentifier(); - Database database = getCurrentDatabase(databaseId); - - //上下文中存在 则不归还 直接返回 - //上下文中不存在才归还 - if (database == null) { - db.release(); - } + //上下文中存在 则不归还 直接返回 + //上下文中不存在才归还 + if (database == null) { + db.release(); } } @@ -221,7 +200,7 @@ public class DatabaseManager { } if(dbMap.get(databaseId)==null) - log.info("\nthread ["+Thread.currentThread().getId()+"] context has no [Database],will create new [Database]"); + log.info("\nthread ["+Thread.currentThread().getId()+"] context has no [Database]"); else log.info("\nthread ["+Thread.currentThread().getId()+"] get Database from context,databaseId:"+databaseId); return dbMap.get(databaseId); @@ -239,8 +218,11 @@ public class DatabaseManager { dbMap = new ConcurrentHashMap<>(); this.databaseThreadLocal.set(dbMap); } - log.info("\n线程"+Thread.currentThread().getId()+"设置当前上下文的database,databaseId:"+databaseId); + dbMap.put(databaseId, database); + if(log.isDebugEnabled()){ + log.debug("\nthread ["+Thread.currentThread().getId()+"] set Database to current context,databaseId:"+databaseId); + } } /**