java c3p0:如何配置 autoreconnect=true?

发布于 2024-09-14 17:05:07 字数 1441 浏览 6 评论 0原文

我正在使用 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;
}

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

It seems that after the connection as timed out in my MySQL server my application stops working with a suggestion to configure 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百善笑为先 2024-09-21 17:05:07

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

# c3p0.properties
c3p0.testConnectionOnCheckout=true

有关更多文档,请参阅这个

这篇文章也可能会有所帮助。

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

# c3p0.properties
c3p0.testConnectionOnCheckout=true

For further documentation refer to this.

This post might be helpful also.

梦中的蝴蝶 2024-09-21 17:05:07

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

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

您可以使用外部属性文件,或通过代码添加:例如如何使用数据源创建自定义池数据源并添加自定义选项(更多示例位于 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 ); 

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.

You got all C3p0 information and samples here 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)

// 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 ); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文