java c3p0:如何配置autoreconnect=true?
我正在用Java编写一个red5应用程序 我使用c3p0进行数据库交互 在我的MySQL服务器中连接超时后,我的应用程序似乎停止工作,并建议配置autoreconnect=true 我怎样才能做到 这是我用来创建数据源的函数:java c3p0:如何配置autoreconnect=true?,java,mysql,red5,c3p0,Java,Mysql,Red5,C3p0,我正在用Java编写一个red5应用程序 我使用c3p0进行数据库交互 在我的MySQL服务器中连接超时后,我的应用程序似乎停止工作,并建议配置autoreconnect=true 我怎样才能做到 这是我用来创建数据源的函数: private ComboPooledDataSource _createDataSource() { Properties props = new Properties(); // Looks for the file 'database.propert
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;
}
创建一个文件
c3p0.properties
,该文件必须位于类路径的根目录中:
# c3p0.properties
c3p0.testConnectionOnCheckout=true
有关更多文档,请参阅
也可能有帮助。属性autoreconnect不是C3p0对象的一部分。要使用C3p0池,建议配置其他选项(如TestConnectionOnOnCheckout)和使用工厂 你这里有所有的C3p0信息和样品 您可以使用和外部属性文件,或按代码添加:例如,如何使用数据源创建自定义池数据源并添加自定义选项(C3p0文档url中的更多示例)
对好多了。MYSQL官方强烈建议不要使用autoreconnect,因为它可能导致损坏问题。对不起,没有链接-几年前看到过这个。有人能解释一下什么是连接签入和签出吗?
// 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 );