看了一堆博客文章,更改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");
连接加载正常,搞定