From 53ee1d5b093a4a30761417ad7b586eb420c318b0 Mon Sep 17 00:00:00 2001 From: renxb Date: Mon, 2 Feb 2026 16:12:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96databasemanager=E7=9A=84?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E9=94=81=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iec/edp/caf/database/DatabaseManager.java | 95 ++++++++----------- .../multilangcolumn/CountryWithEmbeded.java | 6 +- 2 files changed, 44 insertions(+), 57 deletions(-) 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 da5f068cb..850f60a75 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,stacktrace: "+ex); - } - //上下文中不存在 则组建新的Database对象 - if (database == null) { + 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()) { - 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(); + if(log.isDebugEnabled()) { + log.debug("\nthread ["+Thread.currentThread().getId()+"] is invoking [end] method: Database context is already closed, databaseId:"+databaseId); } - - 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; } /** @@ -221,7 +203,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 +221,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); + } } /** diff --git a/caf-data/caf-data-jpa/src/test/java/io/iec/edp/caf/data/multilangcolumn/CountryWithEmbeded.java b/caf-data/caf-data-jpa/src/test/java/io/iec/edp/caf/data/multilangcolumn/CountryWithEmbeded.java index 02522bb2c..2dd967f1f 100644 --- a/caf-data/caf-data-jpa/src/test/java/io/iec/edp/caf/data/multilangcolumn/CountryWithEmbeded.java +++ b/caf-data/caf-data-jpa/src/test/java/io/iec/edp/caf/data/multilangcolumn/CountryWithEmbeded.java @@ -26,6 +26,10 @@ import java.util.List; @Entity @Table(name = "country2") public class CountryWithEmbeded { + + @Embedded + private CAFMultiLanguageColumn name; + public CountryWithEmbeded(){ } public CountryWithEmbeded(String name2_final){ @@ -42,8 +46,6 @@ public class CountryWithEmbeded { private String id; private String code; - @Embedded - private CAFMultiLanguageColumn name; @Embedded private CAFMultiLanguageColumn name2; -- Gitee From 4132622d29d9fb7bbd73942a1d234583220bc703 Mon Sep 17 00:00:00 2001 From: renxb Date: Mon, 2 Feb 2026 17:09:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96databasemanager=E7=9A=84?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E9=94=81=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/iec/edp/caf/database/DatabaseManager.java | 15 ++++++--------- .../data/multilangcolumn/CountryWithEmbeded.java | 6 ++---- 2 files changed, 8 insertions(+), 13 deletions(-) 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 850f60a75..29d87f267 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 @@ -176,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(); } } diff --git a/caf-data/caf-data-jpa/src/test/java/io/iec/edp/caf/data/multilangcolumn/CountryWithEmbeded.java b/caf-data/caf-data-jpa/src/test/java/io/iec/edp/caf/data/multilangcolumn/CountryWithEmbeded.java index 2dd967f1f..02522bb2c 100644 --- a/caf-data/caf-data-jpa/src/test/java/io/iec/edp/caf/data/multilangcolumn/CountryWithEmbeded.java +++ b/caf-data/caf-data-jpa/src/test/java/io/iec/edp/caf/data/multilangcolumn/CountryWithEmbeded.java @@ -26,10 +26,6 @@ import java.util.List; @Entity @Table(name = "country2") public class CountryWithEmbeded { - - @Embedded - private CAFMultiLanguageColumn name; - public CountryWithEmbeded(){ } public CountryWithEmbeded(String name2_final){ @@ -46,6 +42,8 @@ public class CountryWithEmbeded { private String id; private String code; + @Embedded + private CAFMultiLanguageColumn name; @Embedded private CAFMultiLanguageColumn name2; -- Gitee