Commit 09bf59de authored by GregDThomas's avatar GregDThomas Committed by Guus der Kinderen

OF-1419 revisited: ensure that the embedded database works too.

parent 6c52d409
...@@ -16,6 +16,14 @@ ...@@ -16,6 +16,14 @@
package org.jivesoftware.database; package org.jivesoftware.database;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -25,8 +33,6 @@ import java.io.IOException; ...@@ -25,8 +33,6 @@ import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.DriverManager;
import java.util.Properties;
/** /**
* A connection provider for the embedded hsqlDB database. The database file is stored at * A connection provider for the embedded hsqlDB database. The database file is stored at
...@@ -40,10 +46,8 @@ public class EmbeddedConnectionProvider implements ConnectionProvider { ...@@ -40,10 +46,8 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
private static final Logger Log = LoggerFactory.getLogger(EmbeddedConnectionProvider.class); private static final Logger Log = LoggerFactory.getLogger(EmbeddedConnectionProvider.class);
private Properties settings;
private String serverURL; private String serverURL;
private String driver = "org.hsqldb.jdbcDriver"; private PoolingDataSource<PoolableConnection> dataSource;
private String proxoolURL;
public EmbeddedConnectionProvider() { public EmbeddedConnectionProvider() {
} }
...@@ -55,13 +59,10 @@ public class EmbeddedConnectionProvider implements ConnectionProvider { ...@@ -55,13 +59,10 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
try { if (dataSource == null) {
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); throw new SQLException("Check HSQLDB properties; data source was not be initialised");
return DriverManager.getConnection(proxoolURL, settings);
}
catch (ClassNotFoundException e) {
throw new SQLException("EmbeddedConnectionProvider: Unable to find driver: "+e);
} }
return dataSource.getConnection();
} }
@Override @Override
...@@ -78,13 +79,16 @@ public class EmbeddedConnectionProvider implements ConnectionProvider { ...@@ -78,13 +79,16 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
catch (IOException ioe) { catch (IOException ioe) {
Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe); Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe);
} }
proxoolURL = "proxool.openfire:"+driver+":"+serverURL; final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(serverURL, "sa", "");
settings = new Properties(); final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
settings.setProperty("proxool.maximum-connection-count", "25"); poolableConnectionFactory.setMaxConnLifetimeMillis((long) (0.5 * JiveConstants.DAY));
settings.setProperty("proxool.minimum-connection-count", "3");
settings.setProperty("proxool.maximum-connection-lifetime", Integer.toString((int)(86400000 * 0.5))); final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
settings.setProperty("user", "sa"); poolConfig.setMinIdle(3);
settings.setProperty("password", ""); poolConfig.setMaxTotal(25);
final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig);
poolableConnectionFactory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
} }
@Override @Override
...@@ -111,13 +115,11 @@ public class EmbeddedConnectionProvider implements ConnectionProvider { ...@@ -111,13 +115,11 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
finally { finally {
DbConnectionManager.closeConnection(pstmt, con); DbConnectionManager.closeConnection(pstmt, con);
} }
// Blank out the settings
settings = null;
} }
@Override @Override
public void finalize() throws Throwable { public void finalize() throws Throwable {
super.finalize();
destroy(); destroy();
super.finalize();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment