更改C3P0连接池的XML配置

看了一堆博客文章,更改c3p0的xml文件的路径使用以下方式

System.setProperty("com.mchange.v2.c3p0.cfg.xml","resources/settings/c3p0-config.xml"); 
按照以上方式配置后一直报 caused by: Connections could not be acquired from the underlying database! 

检查用户名、密码、连接字符串也没问题

最后找到了c3p0加载配置文件路径的类DefaultC3P0ConfigFinder

if ( cfgFile.startsWith( CLASSLOADER_RESOURCE_PREFIX ) )
			{
			    ClassLoader cl = this.getClass().getClassLoader();
			    String rsrcPath = cfgFile.substring( CLASSLOADER_RESOURCE_PREFIX.length() );

			    // eliminate leading slash because ClassLoader.getResource
			    // is always absolute and does not expect a leading slash
			    if (rsrcPath.startsWith("/")) 
				rsrcPath = rsrcPath.substring(1);

			    is = cl.getResourceAsStream( rsrcPath );
			    if ( is == null )
				throw new FileNotFoundException("Specified ClassLoader resource '" + rsrcPath + "' could not be found. " +
								"[ Found in configuration: " + XML_CFG_FILE_KEY + '=' + cfgFile + " ]");

			    mbOverrideWarning( "resource", rsrcPath );
			}
			else
			{
			    is = new BufferedInputStream( new FileInputStream( cfgFile ) );
			    mbOverrideWarning( "file", cfgFile );
			}

这个类在加载文件时只有try finally没有捕捉异常,所以在调用的时候也看不到配置文件加载不上的错误,不知道这么做为啥,有时间再看。

最后改成

System.setProperty("com.mchange.v2.c3p0.cfg.xml","classloader:/resources/settings/c3p0-config.xml");
连接加载正常,搞定


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值