java c3p0:我如何配置autoreconnect = true?

ufk*_*ufk 8 java mysql red5 c3p0

我正在使用Java编写一个red5应用程序,我正在使用c3p0进行数据库交互.

似乎在我的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;
}
Run Code Online (Sandbox Code Playgroud)

Rom*_*eau 6

创建一个c3p0.properties必须位于类路径根目录中的文件:

# c3p0.properties
c3p0.testConnectionOnCheckout=true
Run Code Online (Sandbox Code Playgroud)

如需进一步资料请参阅.

这篇文章也许有帮助.