Restore old driver logic

This commit is contained in:
James Agnew 2018-09-17 13:46:21 -04:00
parent fd5854ea85
commit 798982773a
1 changed files with 17 additions and 26 deletions

View File

@ -1,17 +1,18 @@
package ca.uhn.fhir.jpa.migrate; package ca.uhn.fhir.jpa.migrate;
import ca.uhn.fhir.context.ConfigurationException;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.DisposableBean; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource; import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate; import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.sql.DataSource;
import java.sql.Driver; import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;
/*- /*-
* #%L * #%L
@ -49,6 +50,7 @@ public enum DriverTypeEnum {
; ;
private static final Logger ourLog = LoggerFactory.getLogger(DriverTypeEnum.class);
private String myDriverClassName; private String myDriverClassName;
private boolean myDerby; private boolean myDerby;
@ -60,21 +62,15 @@ public enum DriverTypeEnum {
myDerby = theDerby; myDerby = theDerby;
} }
public ConnectionProperties newConnectionProperties(String theUrl, String theUsername, String thePassword) { public ConnectionProperties newConnectionProperties(String theUrl, String theUsername, String thePassword) {
Driver driver = null; SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
try { dataSource.setAutoCommit(false);
driver = (Driver) Class.forName(myDriverClassName).newInstance(); dataSource.setDriverClassName(myDriverClassName);
} catch (Exception e) {
throw new ConfigurationException("Failed to initialize driver: " + myDriverClassName + ": " + e.toString());
}
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(driver);
dataSource.setUrl(theUrl); dataSource.setUrl(theUrl);
dataSource.setUsername(theUsername); dataSource.setUsername(theUsername);
dataSource.setPassword(thePassword); dataSource.setPassword(thePassword);
dataSource.setSuppressClose(true);
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource); transactionManager.setDataSource(dataSource);
@ -91,12 +87,13 @@ public enum DriverTypeEnum {
public static class ConnectionProperties { public static class ConnectionProperties {
private final DriverTypeEnum myDriverType; private final DriverTypeEnum myDriverType;
private final DataSource myDataSource; private final SingleConnectionDataSource myDataSource;
private final TransactionTemplate myTxTemplate; private final TransactionTemplate myTxTemplate;
/** /**
* Constructor * Constructor
*/ */
public ConnectionProperties(DataSource theDataSource, TransactionTemplate theTxTemplate, DriverTypeEnum theDriverType) { public ConnectionProperties(SingleConnectionDataSource theDataSource, TransactionTemplate theTxTemplate, DriverTypeEnum theDriverType) {
Validate.notNull(theDataSource); Validate.notNull(theDataSource);
Validate.notNull(theTxTemplate); Validate.notNull(theTxTemplate);
Validate.notNull(theDriverType); Validate.notNull(theDriverType);
@ -111,7 +108,7 @@ public enum DriverTypeEnum {
} }
@Nonnull @Nonnull
public DataSource getDataSource() { public SingleConnectionDataSource getDataSource() {
return myDataSource; return myDataSource;
} }
@ -128,13 +125,7 @@ public enum DriverTypeEnum {
} }
public void close() { public void close() {
if (myDataSource instanceof DisposableBean) { myDataSource.destroy();
try {
((DisposableBean) myDataSource).destroy();
} catch (Exception theE) {
// ignore
}
}
} }
} }
} }