java c3p0:如何配置 autoreconnect=true?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个文件
c3p0.properties
,该文件必须位于类路径的根目录中:有关更多文档,请参阅这个。
这篇文章也可能会有所帮助。
Create a file
c3p0.properties
which must be in the root of the classpath:For further documentation refer to this.
This post might be helpful also.
autoreconnect 属性不是 C3p0 对象的一部分要使用 C3P0 池,建议配置其他选项(例如 testConnectionOnCheckout)并使用 Factory。
您在此处获取了所有 C3p0 信息和示例 http://www.mchange.com/projects/ c3p0/index.html
您可以使用外部属性文件,或通过代码添加:例如如何使用数据源创建自定义池数据源并添加自定义选项(更多示例位于 C3p0 文档 url 中)
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)