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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3511422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:11:02  来源:igfitidea点击:

java c3p0: how can i configure autoreconnect=true?

javamysqlred5c3p0

提问by ufk

I'm writing a red5 application using Java and I'm using the c3p0 for the database interaction.

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

It seems that after the connection as timed out in my MySQL server my application stops working with a suggestion to configure autoreconnect=true.

似乎在我的 MySQL 服务器中连接超时后,我的应用程序停止工作,建议配置 autoreconnect=true。

how can i do so?

我该怎么做?

this is the function that i use to create datasource:

这是我用来创建数据源的函数:

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;
}

采纳答案by Romain Hippeau

Create a file c3p0.propertieswhich must be in the root of the classpath:

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

# c3p0.properties
c3p0.testConnectionOnCheckout=true

For further documentation refer to this.

有关更多文档,请参阅

This postmight be helpful also.

这篇文章也可能有帮助。

回答by Dubas

The property autoreconnect is no part of C3p0 objectTo use a C3P0 pool its recommended to configure other options (like testConnectionOnCheckout) , and to use a Factory.

属性 autoreconnect 不是 C3p0 对象的一部分要使用 C3P0 池,建议配置其他选项(如testConnectionOnCheckout),并使用工厂。

You got all C3p0 information and samples here http://www.mchange.com/projects/c3p0/index.html

您在此处获得了所有 C3p0 信息和示例http://www.mchange.com/projects/c3p0/index.html

You can use and external properties file, or add by code: for example how to create a custom Pooled datasource using a datasource and adding custom options (more samples in the C3p0 documentation url)

您可以使用外部属性文件,或通过代码添加:例如如何使用数据源创建自定义池化数据源并添加自定义选项(更多示例在 C3p0 文档 url 中)

// 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 );