Skip to content

Commit

Permalink
support jdbc connection pools #366
Browse files Browse the repository at this point in the history
  • Loading branch information
baisui1981 committed Oct 7, 2024
1 parent 955b433 commit 27aeb26
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public synchronized SetPluginsResult setPlugins(IPluginContext pluginContext, Op
if (this.plugins != null) {
this.plugins.forEach((plugin) -> {
if (plugin instanceof AfterPluginSaved) {
((AfterPluginSaved) plugin).afterSaved( pluginContext, context);
((AfterPluginSaved) plugin).afterSaved(pluginContext, context);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import com.qlangtech.tis.plugin.IRepositoryTargetFile;
import com.qlangtech.tis.runtime.module.misc.IMessageHandler;
import com.qlangtech.tis.runtime.module.misc.impl.AdapterMessageHandler;
import com.qlangtech.tis.util.RobustReflectionConverter2;
import com.qlangtech.tis.util.RobustReflectionConverter2.PluginMetas;
import com.qlangtech.tis.web.start.TisAppLaunch;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -135,7 +138,7 @@ public void vistDbName(IProcess p) throws Exception {
}
}

public static final int expireSec = 15;
public static final int expireSec = TisAppLaunch.isTestMock() ? 200 : 15;

public void vistDbURL(boolean resolveHostIp, IDbUrlProcess urlProcess) {
vistDbURL(resolveHostIp, expireSec, urlProcess);
Expand Down Expand Up @@ -276,6 +279,7 @@ public boolean vistDbURL(boolean resolveHostIp, IDbUrlProcess urlProcess
});
try {
final JDBCConnectionPool connectionPool = JDBCConnection.connectionPool.get();
final PluginMetas pluginMetas = RobustReflectionConverter2.usedPluginInfo.get();
int dbCount = 0;
for (Map.Entry<String, List<String>> entry : this.getDbEnum().entrySet()) {
dbCount += entry.getValue().size();
Expand All @@ -302,6 +306,8 @@ public boolean vistDbURL(boolean resolveHostIp, IDbUrlProcess urlProcess
if (connectionPool != null) {
JDBCConnection.connectionPool.set(connectionPool);
}
RobustReflectionConverter2.usedPluginInfo.set(pluginMetas);

fjdbcUrl.set(jdbcUrl);
urlProcess.visit((facade ? name : dbName), dbHost, (jdbcUrl));
} catch (Throwable e) {
Expand All @@ -311,7 +317,7 @@ public boolean vistDbURL(boolean resolveHostIp, IDbUrlProcess urlProcess
if (connectionPool != null) {
JDBCConnection.connectionPool.remove();
}
// IRepositoryTargetFile.TARGET_FILE_CONTEXT.remove();
RobustReflectionConverter2.usedPluginInfo.remove();
}
});
if (facade) {
Expand Down Expand Up @@ -422,7 +428,7 @@ public interface IProcess {
* @param dbName
* @return
*/
boolean visit(DBConfig config, String jdbcUrl, String ip, String dbName) throws Exception;
boolean visit(DBConfig config, String jdbcUrl, String ip, String dbName) throws Exception;
}

public interface IDbUrlProcess {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected <C extends BaseDataSourceFactoryDescriptor> Class<C> getExpectDesClass
protected void validateConnection(String jdbcUrl, IConnProcessor p) throws TableNotFoundException {
JDBCConnection conn = null;
try {
conn = this.createConnection(jdbcUrl, true); // getConnection(jdbcUrl, true);
conn = this.getConnection(jdbcUrl, true); // getConnection(jdbcUrl, true);
p.vist(conn);
} catch (TableNotFoundException e) {
throw e;
Expand Down Expand Up @@ -183,8 +183,13 @@ public final JDBCConnection getConnection(String jdbcUrl, boolean verify) throws
if (connectionPool != null) {
conn = connectionPool.getConnection(jdbcUrl, verify);
if (conn == null) {
conn = createConnection(jdbcUrl, verify);
return connectionPool.setConnection(jdbcUrl, verify, conn);
return connectionPool.getConnection(jdbcUrl, verify, (url) -> {
try {
return createConnection(jdbcUrl, verify);
} catch (SQLException e) {
throw new RuntimeException(e);
}
});
} else {
return conn;
}
Expand All @@ -201,11 +206,11 @@ public final JDBCConnection getConnection(String jdbcUrl, boolean verify) throws
* @return
* @throws SQLException
*/
public JDBCConnection createConnection(String jdbcUrl, boolean verify) throws SQLException {
protected JDBCConnection createConnection(String jdbcUrl, boolean verify) throws SQLException {
throw new UnsupportedOperationException("class:" + this.getClass().getName() + ",jdbcUrl:" + jdbcUrl);
}

public JDBCConnection getConnection(String jdbcUrl, boolean usingPool, boolean verify) throws SQLException {
public JDBCConnection getConnection(String jdbcUrl, boolean usingPool, boolean verify) throws SQLException {
return this.getConnection(jdbcUrl, verify);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

package com.qlangtech.tis.plugin.ds;

import java.util.function.Function;

/**
* @author: 百岁(baisui@qlangtech.com)
* @create: 2024-10-05 19:40
**/
public abstract class JDBCConnectionPool {
public abstract JDBCConnection getConnection(String jdbcUrl, boolean verify);

public abstract JDBCConnection setConnection(String jdbcUrl, boolean verify, JDBCConnection conn);
public abstract JDBCConnection getConnection(String jdbcUrl, boolean verify, Function<String, JDBCConnection> mappingFunction);
//public abstract JDBCConnection setConnection(String jdbcUrl, boolean verify, JDBCConnection conn);
}
4 changes: 2 additions & 2 deletions tis-web-config/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ tis.datasource.type=derby
tis.datasource.dbname=tis_console_db


assemble.host=192.168.28.107
tis.host=192.168.28.107
assemble.host=192.168.28.109
tis.host=192.168.28.109



0 comments on commit 27aeb26

Please sign in to comment.