preventing reuse of closed jdbc connections in dao factories stored in context
This commit is contained in:
parent
2957d66594
commit
e3e7e1ebd6
3 changed files with 73 additions and 10 deletions
|
@ -15,6 +15,11 @@ public class DatasetWrapper {
|
||||||
this.dataset = dataset;
|
this.dataset = dataset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DatasetWrapper(Dataset dataset, SDBConnection conn) {
|
||||||
|
this.dataset = dataset;
|
||||||
|
this.conn = conn;
|
||||||
|
}
|
||||||
|
|
||||||
public Dataset getDataset() {
|
public Dataset getDataset() {
|
||||||
if (!closed) {
|
if (!closed) {
|
||||||
return dataset;
|
return dataset;
|
||||||
|
|
|
@ -2,9 +2,17 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
import org.apache.commons.dbcp.BasicDataSource;
|
||||||
|
|
||||||
import com.hp.hpl.jena.query.Dataset;
|
import com.hp.hpl.jena.query.Dataset;
|
||||||
|
import com.hp.hpl.jena.sdb.SDBFactory;
|
||||||
|
import com.hp.hpl.jena.sdb.Store;
|
||||||
|
import com.hp.hpl.jena.sdb.StoreDesc;
|
||||||
|
import com.hp.hpl.jena.sdb.sql.SDBConnection;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
|
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
|
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
|
||||||
|
@ -32,11 +40,31 @@ public class WebappDaoFactorySDB extends WebappDaoFactoryJena {
|
||||||
* @param ontModelSelector
|
* @param ontModelSelector
|
||||||
* @param dataset
|
* @param dataset
|
||||||
*/
|
*/
|
||||||
public WebappDaoFactorySDB(OntModelSelector ontModelSelector, Dataset dataset, String defaultNamespace, HashSet<String> nonuserNamespaces, String[] preferredLanguages) {
|
public WebappDaoFactorySDB(OntModelSelector ontModelSelector,
|
||||||
|
Dataset dataset,
|
||||||
|
String defaultNamespace,
|
||||||
|
HashSet<String> nonuserNamespaces,
|
||||||
|
String[] preferredLanguages) {
|
||||||
super(ontModelSelector, defaultNamespace, nonuserNamespaces, preferredLanguages);
|
super(ontModelSelector, defaultNamespace, nonuserNamespaces, preferredLanguages);
|
||||||
this.dwf = new StaticDatasetFactory(dataset);
|
this.dwf = new StaticDatasetFactory(dataset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For use when any Dataset access should get a temporary DB connection
|
||||||
|
* from a pool
|
||||||
|
* @param ontModelSelector
|
||||||
|
* @param dataset
|
||||||
|
*/
|
||||||
|
public WebappDaoFactorySDB(OntModelSelector ontModelSelector,
|
||||||
|
BasicDataSource bds,
|
||||||
|
StoreDesc storeDesc,
|
||||||
|
String defaultNamespace,
|
||||||
|
HashSet<String> nonuserNamespaces,
|
||||||
|
String[] preferredLanguages) {
|
||||||
|
super(ontModelSelector, defaultNamespace, nonuserNamespaces, preferredLanguages);
|
||||||
|
this.dwf = new ReconnectingDatasetFactory(bds, storeDesc);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IndividualDao getIndividualDao() {
|
public IndividualDao getIndividualDao() {
|
||||||
if (entityWebappDao != null)
|
if (entityWebappDao != null)
|
||||||
|
@ -71,14 +99,44 @@ public class WebappDaoFactorySDB extends WebappDaoFactoryJena {
|
||||||
|
|
||||||
private class StaticDatasetFactory implements DatasetWrapperFactory {
|
private class StaticDatasetFactory implements DatasetWrapperFactory {
|
||||||
|
|
||||||
private Dataset dataset;
|
private Dataset _dataset;
|
||||||
|
|
||||||
public StaticDatasetFactory (Dataset dataset) {
|
public StaticDatasetFactory (Dataset dataset) {
|
||||||
this.dataset = dataset;
|
_dataset = dataset;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatasetWrapper getDatasetWrapper() {
|
public DatasetWrapper getDatasetWrapper() {
|
||||||
return new DatasetWrapper(dataset);
|
return new DatasetWrapper(_dataset);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ReconnectingDatasetFactory implements DatasetWrapperFactory {
|
||||||
|
|
||||||
|
private BasicDataSource _bds;
|
||||||
|
private StoreDesc _storeDesc;
|
||||||
|
private Dataset _dataset;
|
||||||
|
private Connection _conn;
|
||||||
|
|
||||||
|
public ReconnectingDatasetFactory(BasicDataSource bds, StoreDesc storeDesc) {
|
||||||
|
_bds = bds;
|
||||||
|
_storeDesc = storeDesc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatasetWrapper getDatasetWrapper() {
|
||||||
|
try {
|
||||||
|
if ((_dataset != null) && (_conn != null) && (!_conn.isClosed())) {
|
||||||
|
return new DatasetWrapper(_dataset);
|
||||||
|
} else {
|
||||||
|
_conn = _bds.getConnection();
|
||||||
|
SDBConnection conn = new SDBConnection(_conn) ;
|
||||||
|
Store store = SDBFactory.connectStore(conn, _storeDesc);
|
||||||
|
_dataset = SDBFactory.connectDataset(store);
|
||||||
|
return new DatasetWrapper(_dataset);
|
||||||
|
}
|
||||||
|
} catch (SQLException sqe) {
|
||||||
|
throw new RuntimeException("Unable to connect to database", sqe);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -225,20 +225,20 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
|
||||||
//log.info("Test query returned " + qe.execConstruct().size() + " statements");
|
//log.info("Test query returned " + qe.execConstruct().size() + " statements");
|
||||||
|
|
||||||
sce.getServletContext().setAttribute("baseOntModel", memModel);
|
sce.getServletContext().setAttribute("baseOntModel", memModel);
|
||||||
WebappDaoFactory baseWadf = new WebappDaoFactorySDB(baseOms, dataset, defaultNamespace, null, null);
|
WebappDaoFactory baseWadf = new WebappDaoFactorySDB(baseOms, bds, storeDesc, defaultNamespace, null, null);
|
||||||
//WebappDaoFactory baseWadf = new WebappDaoFactoryJena(baseOms, defaultNamespace, null, null);
|
//WebappDaoFactory baseWadf = new WebappDaoFactorySDB(baseOms, dataset, defaultNamespace, null, null);
|
||||||
sce.getServletContext().setAttribute("assertionsWebappDaoFactory",baseWadf);
|
sce.getServletContext().setAttribute("assertionsWebappDaoFactory",baseWadf);
|
||||||
|
|
||||||
sce.getServletContext().setAttribute("inferenceOntModel", inferenceModel);
|
sce.getServletContext().setAttribute("inferenceOntModel", inferenceModel);
|
||||||
WebappDaoFactory infWadf = new WebappDaoFactorySDB(inferenceOms, dataset, defaultNamespace, null, null);
|
WebappDaoFactory infWadf = new WebappDaoFactorySDB(inferenceOms, bds, storeDesc, defaultNamespace, null, null);
|
||||||
//WebappDaoFactory infWadf = new WebappDaoFactoryJena(inferenceOms, defaultNamespace, null, null);
|
//WebappDaoFactory infWadf = new WebappDaoFactorySDB(inferenceOms, dataset, defaultNamespace, null, null);
|
||||||
sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf);
|
sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf);
|
||||||
|
|
||||||
OntModel masterUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
|
OntModel masterUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM,
|
||||||
ModelFactory.createUnion(unionABoxModel, unionTBoxModel));
|
ModelFactory.createUnion(unionABoxModel, unionTBoxModel));
|
||||||
sce.getServletContext().setAttribute("jenaOntModel", masterUnion);
|
sce.getServletContext().setAttribute("jenaOntModel", masterUnion);
|
||||||
WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, dataset, defaultNamespace, null, null);
|
WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, bds, storeDesc, defaultNamespace, null, null);
|
||||||
//WebappDaoFactory wadf = new WebappDaoFactoryJena(unionOms, defaultNamespace, null, null);
|
//WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, dataset, defaultNamespace, null, null);
|
||||||
sce.getServletContext().setAttribute("webappDaoFactory",wadf);
|
sce.getServletContext().setAttribute("webappDaoFactory",wadf);
|
||||||
|
|
||||||
sce.getServletContext().setAttribute("unionOntModelSelector", unionOms); //assertions and inferences
|
sce.getServletContext().setAttribute("unionOntModelSelector", unionOms); //assertions and inferences
|
||||||
|
|
Loading…
Add table
Reference in a new issue