关于jfinal连接多sqlserver多数据源的问题

基本情况是:我有个需求是需要连接多个不同类型和不同实例的数据库去对比数据,但是前面两个数据源可以访问到,第三个就是不行,麻烦有碰到这种情况的大神释疑解惑一下,感激不尽

#database config
dbType=mysql
jdbcUrl = jdbc:mysql://10.1.1.8/jfinaldemo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
user = root
password =111111
devMode =true

#This data is for BSERP3,the dbType is SqlServer
dbType2 = SqlServer
jdbcUrl2 = jdbc:sqlserver://218.16.212.43:8437;Database=BSERP3
user2 = sa
password2 = HX&ftmz2018!
driverClass2 = com.microsoft.sqlserver.jdbc.SQLServerDriver
devMode2 = true

#This data is for U8CLOUD,the dbType is SqlServer
dbType3 = SqlServer
jdbcUrl3 = jdbc:sqlserver://127.0.0.1:1433;Database=U8CLOUD
user3 = sa
password3 = sa
driverClass3 = com.microsoft.sqlserver.jdbc.SQLServerDriver
devMode3 = true

上面是配置文件,下面是java代码

@Override
	public void configPlugin(Plugins me) {
		//配置数据库连接池插件(Mysql数据源配置)
		C3p0Plugin c3p0Plugin=new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));//这里默认的是使用Mysql的Driver,所以这里不需要传Driver地址
		System.out.println("****************************设置Mysql数据库连接池成功**************************************");
		//orm映射 配置ActiveRecord插件
		ActiveRecordPlugin arp=new ActiveRecordPlugin("mysql",c3p0Plugin);//当有多个数据源时,这里需要给每个数据源起别名
		System.out.println("*******************************Mysql配置ActiveRecordPlugin成功***********************************");
		arp.setShowSql(PropKit.getBoolean("devMode"));
		arp.setDialect(new MysqlDialect());//设置数据库方言,这里设置的是mysql的方言
		System.out.println("***************************设置Mysql方言成功***************************************");
		_MappingKit.mapping(arp);//设置主键映射
		System.out.println("*****************************设置Mysql主键映射成功*************************************\n\n");
		me.add(c3p0Plugin);
		me.add(arp);
		
			    
		//SqlServer数据源配置(这个数据库是BSERP3的数据库)
		C3p0Plugin c3p0Plugin2=new C3p0Plugin(PropKit.get("jdbcUrl2"), PropKit.get("user2"), PropKit.get("password2"),PropKit.get("driverClass2"));
		System.out.println("****************************设置SqlServer1数据库连接池成功**************************************");
		// DruidPlugin dpSqlserver2 = new DruidPlugin(" jdbc:sqlserver:// 218.16.212.43:8257;Database=BSERP3","sa"," HX&ftmz2018!","com.microsoft.sqlserver.jdbc.SQLServerDriver");
		ActiveRecordPlugin arp2=new ActiveRecordPlugin("sqlserver1",c3p0Plugin2);//当有多个数据源时,这里需要给每个数据源起别名
		System.out.println("*******************************SqlServer1配置ActiveRecordPlugin成功***********************************");
		arp2.setShowSql(PropKit.getBoolean("devMode2"));
		arp2.setDialect(new SqlServerDialect());
		System.out.println("***************************设置SqlServer1方言成功***************************************\n\n");
		//_MappingKit.sqlServerMapping(arp2);//设置主键映射
		me.add(c3p0Plugin2);
		me.add(arp2);
				
	
		//SqlServer数据源配置(这个数据库是U8CLOUD的数据库)
		C3p0Plugin c3p0Plugin3=new C3p0Plugin(PropKit.get("jdbcUrl3"), PropKit.get("user3"), PropKit.get("password3"),PropKit.get("driverClass3"));
		System.out.println("****************************设置SqlServer2数据库连接池成功**************************************");
		ActiveRecordPlugin arp3=new ActiveRecordPlugin("sqlserver2",c3p0Plugin3);//当有多个数据源时,这里需要给每个数据源起别名
		System.out.println("*******************************SqlServer2配置ActiveRecordPlugin成功***********************************");
		arp3.setShowSql(PropKit.getBoolean("devMode3"));
		arp3.setDialect(new SqlServerDialect());
		System.out.println("***************************设置SqlServer2方言成功***************************************");
		//_MappingKit.sqlServerMapping(arp2);//设置主键映射
		me.add(c3p0Plugin3);
		me.add(arp3);
		
		
		ActivitiPlugin acitivitiPlugin = new ActivitiPlugin();//工作流插件
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);//Shiro插件
	    shiroPlugin.setLoginUrl("/admin/login");//登陆url:未验证成功跳转
	    shiroPlugin.setSuccessUrl("/admin/index");//登陆成功url:验证成功自动跳转
	    shiroPlugin.setUnauthorizedUrl("/admin/login/needPermission");//授权url:未授权成功自动跳转
	    //添加到插件列表中
	    me.add(acitivitiPlugin);
	    me.add(shiroPlugin);
	}

然后报错:

2018-06-22 18:02:47,653 WARN  [com.mchange.v2.async.ThreadPoolAsynchronousRunner] - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@604bbc87 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
2018-06-22 18:02:47,663 WARN  [com.mchange.v2.async.ThreadPoolAsynchronousRunner] - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@604bbc87 -- APPARENT DEADLOCK!!! Complete Status: 
	Managed Threads: 3
	Active Threads: 3
	Active Tasks: 
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@2fbc54ef (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@756c2f64 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@119b8a76 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
	Pending Tasks: 
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@7a9f5201
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@71c0fe8b
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@35879205
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5783a1b7
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@2ee2244
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@600b9fdc
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1b2c55c7
Pool thread stack traces:
	Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
		java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
		java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
		java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
		java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
		java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
		java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
		java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
		java.net.Socket.connect(Socket.java:579)
		com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:360)
		com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1034)
		com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
		com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
		com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
		com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
		com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
		com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
		com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
		com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
		com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
		com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
	Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
		java.lang.Thread.sleep(Native Method)
		com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:876)
		com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
		com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
		com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
		com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
		com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
		com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
		com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
		com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
		com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
	Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
		java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
		java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
		java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
		java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
		java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
		java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
		java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
		java.net.Socket.connect(Socket.java:579)
		com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:360)
		com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1034)
		com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
		com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
		com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
		com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
		com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
		com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
		com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
		com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
		com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
		com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
		com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)


评论区

JFinal

2018-06-22 18:11

前两个可以访问,最后一个不可以,而第二个与最后一个都是 sql server 库,显然是粗心造成的,例如第三个数据库配置有错误

用排除法先解决一下,例如,删掉第二个库,只留第一和第三个库,也就是将第三个库当成第二个库用来,因为你前面讲了,你的第二个库是可以用的

再一个你的异常信息中没有 cause 部分,也没有 error , 只有 warn ,这个严格来说不是异常

将 c3p0 换成 DrudiPlugin 即可,具体用法在首页下载 jfinal demo 中的 druid 用法

Hulk-X

2018-06-22 18:19

@JFinal 换了连接池后报这个:
严重: Exception starting filter jfinal
java.lang.NoClassDefFoundError: com/alibaba/druid/pool/DruidDataSource
at com.jfinal.plugin.druid.DruidPlugin.start(DruidPlugin.java:180)
at com.jfinal.core.Config.startPlugins(Config.java:107)
at com.jfinal.core.Config.configJFinal(Config.java:53)
at com.jfinal.core.JFinal.init(JFinal.java:63)
at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:49)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:277)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:103)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4650)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1100)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1618)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: com.alibaba.druid.pool.DruidDataSource
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
... 22 more

JFinal

2018-06-22 18:20

NoClassDefFoundError 已然是 java 基础,没有回答的空间了

Hulk-X

2018-06-22 18:20

@JFinal 主要是这两个sqlserver数据库都是必须要连接的

JFinal

2018-06-22 18:23

@Hulk-X 或许你是新手,虽然这个问题我回答过了不下 1000 次,最后再回答一次:
NoClassDefFoundError 表示你的 druid 的 jar 包有问题,或者多个版本的 druid jar 包有冲突

解决办法是先删掉本地 maven 库中的 druid ,让 maven 重下去下载一次

Hulk-X

2018-06-22 18:25

@JFinal 在下就是新手,非常感激波总的回复,我去试试看看

热门反馈

扫码入社