我有1个MySQL URL和4个数据库(方案),它们具有相同的表名,但行数不同(我的意思是要合并它们并不容易)。在我的Java应用程序中,我有一种方法可以根据某些(我自己的)条件选择数据库连接。比在参数中创建带有选定url的对象。但是我认为这是不正确的。
例如
jdbc:mysql://123.456.789.10:3306/First_Database
jdbc:mysql://123.456.789.10:3306/Second_Database
jdbc:mysql://123.456.789.10:3306/Third_Database
jdbc:mysql://123.456.789.10:3306/Fourth_Database
连接示例
public static void createConnectionFirst() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
connect_1 = DriverManager.getConnection(url,user, password);
}// and then same methods for connect_2 connect_3 and connect_4
当我在本地关闭时,出现异常:
14-Nov-2019 17:25:34.774 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
14-Nov-2019 17:25:34.774 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8088"]
14-Nov-2019 17:25:34.879 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
14-Nov-2019 17:25:34.960 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina]
14-Nov-2019 17:25:34.976 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [sc] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.System.currentTimeMillis(Native Method)
org.apache.catalina.webresources.CachedResource.validateResource(CachedResource.java:82)
org.apache.catalina.webresources.Cache.getResource(Cache.java:87)
org.apache.catalina.webresources.StandardRoot.getResource(StandardRoot.java:216)
org.apache.catalina.webresources.StandardRoot.getClassLoaderResource(StandardRoot.java:225)
org.apache.catalina.loader.WebappClassLoaderBase.findResource(WebappClassLenter code hereoaderBase.java:936)
org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1055)
com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.checkContextClassLoaders(AbandonedConnectionCleanupThread.java:96)
com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:69)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
14-Nov-2019 17:25:34.976 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8088"]
14-Nov-2019 17:25:34.976 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
14-Nov-2019 17:25:34.976 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8088"]
14-Nov-2019 17:25:34.976 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]
Disconnected from server
[每当您使用Tomcat时,都可以使用以下命令(例如tomcat手册中的示例)在context.xml文件中预先配置所有数据源:
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest"/>
由于Tomcat负责管理连接,并且它决定根据需要打开或关闭它。
在您的代码中,您只是搜索具有特定名称的数据源并使用它。参见tomcat manual on how to configure datasources