2010-08-18 341 views
7

我正在使用Java編寫一個red5應用程序 ,我正在使用c3p0進行數據庫交互。java c3p0:我如何配置autoreconnect = true?

看來,在我的MySQL服務器超時連接後,我的應用程序停止工作,並建議配置autoreconnect = true。

我該怎麼做?

這是我用於創建數據源的功能:

private ComboPooledDataSource _createDataSource() { 
    Properties props = new Properties(); 
    // Looks for the file 'database.properties' in {TOMCAT_HOME}\webapps\{RED5_HOME}\WEB-INF\ 
    try { 
     FileInputStream in = new FileInputStream(System.getProperty("red5.config_root") + "/database.properties"); 
     props.load(in); 
     in.close(); 
    } catch (IOException ex) { 
     log.error("message: {}", ex.getMessage()); 
     log.error("stack trace: " + ExceptionUtils.getFullStackTrace(ex)); 
     return null; 
    } 

    // It will load the driver String from properties 
    String drivers = props.getProperty("jdbc.drivers"); 
    String url = props.getProperty("jdbc.url"); 
    String username = props.getProperty("jdbc.username"); 
    String password = props.getProperty("jdbc.password"); 

    ComboPooledDataSource cpds = new ComboPooledDataSource(); 
    try { 
     cpds.setDriverClass(drivers); 
    } catch (PropertyVetoException ex) { 
     log.error("message: {}", ex.getMessage()); 
     log.error("stack trace: " + ExceptionUtils.getFullStackTrace(ex)); 
     return null; 
    } 

    cpds.setJdbcUrl(url); 
    cpds.setUser(username); 
    cpds.setPassword(password); 
    cpds.setMaxStatements(180); 

    return cpds; 
} 

回答

6

創建文件c3p0.properties它必須是在類路徑的根:

# c3p0.properties 
c3p0.testConnectionOnCheckout=true 

對於進一步的文檔指this

This post也許也有幫助。

2

屬性autoreconnect不是C3p0對象的一部分使用C3P0池建議配置其他選項(如testConnectionOnCheckout),並使用Factory。

你得到了所有C3P0信息,並在此樣品http://www.mchange.com/projects/c3p0/index.html

您可以使用外部屬性文件,或通過代碼添加:例如如何使用數據源,並添加自定義選項(更多的樣本來創建自定義彙集數據源C3p0文檔網址)

// Your datasource fetched from the properties file 
DataSource ds_unpooled = DataSources.unpooledDataSource("url", "user", "password"); 


// Custom properties to add to the Source 
// See http://www.mchange.com/projects/c3p0/index.html#configuration_properties       

Map overrides = new HashMap(); 
overrides.put("maxStatements", "200");   //Stringified property values work 
overrides.put("maxPoolSize", new Integer(50)); //"boxed primitives" also work 

// Your pooled datasource with all new properties 
ds_pooled = DataSources.pooledDataSource(ds_unpooled, overrides); 
+0

是的!好多了。 autoreconnect正式被MYSQL強烈推薦 - 它可能會導致腐敗問題。對不起,沒有鏈接 - 幾年前見過。 – MJB 2011-04-24 23:39:53

相關問題