天天看點

更改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");
           

連接配接加載正常,搞定