Migrator enhancements

This commit is contained in:
James Agnew 2018-09-17 13:33:44 -04:00
parent c34dfda413
commit fd5854ea85
1 changed files with 26 additions and 10 deletions

View File

@ -1,13 +1,17 @@
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.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.SingleConnectionDataSource; import org.springframework.jdbc.datasource.SimpleDriverDataSource;
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;
/*- /*-
* #%L * #%L
@ -41,7 +45,7 @@ public enum DriverTypeEnum {
ORACLE_12C("oracle.jdbc.OracleDriver", false), ORACLE_12C("oracle.jdbc.OracleDriver", false),
MSSQL_2012("com.microsoft.sqlserver.jdbc.SQLServerDataSource", false), MSSQL_2012("com.microsoft.sqlserver.jdbc.SQLServerDriver", false),
; ;
@ -58,13 +62,19 @@ public enum DriverTypeEnum {
public ConnectionProperties newConnectionProperties(String theUrl, String theUsername, String thePassword) { public ConnectionProperties newConnectionProperties(String theUrl, String theUsername, String thePassword) {
SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
dataSource.setAutoCommit(false); Driver driver = null;
dataSource.setDriverClassName(myDriverClassName); try {
driver = (Driver) Class.forName(myDriverClassName).newInstance();
} 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);
@ -81,12 +91,12 @@ public enum DriverTypeEnum {
public static class ConnectionProperties { public static class ConnectionProperties {
private final DriverTypeEnum myDriverType; private final DriverTypeEnum myDriverType;
private final SingleConnectionDataSource myDataSource; private final DataSource myDataSource;
private final TransactionTemplate myTxTemplate; private final TransactionTemplate myTxTemplate;
/** /**
* Constructor * Constructor
*/ */
public ConnectionProperties(SingleConnectionDataSource theDataSource, TransactionTemplate theTxTemplate, DriverTypeEnum theDriverType) { public ConnectionProperties(DataSource theDataSource, TransactionTemplate theTxTemplate, DriverTypeEnum theDriverType) {
Validate.notNull(theDataSource); Validate.notNull(theDataSource);
Validate.notNull(theTxTemplate); Validate.notNull(theTxTemplate);
Validate.notNull(theDriverType); Validate.notNull(theDriverType);
@ -101,7 +111,7 @@ public enum DriverTypeEnum {
} }
@Nonnull @Nonnull
public SingleConnectionDataSource getDataSource() { public DataSource getDataSource() {
return myDataSource; return myDataSource;
} }
@ -118,7 +128,13 @@ public enum DriverTypeEnum {
} }
public void close() { public void close() {
myDataSource.destroy(); if (myDataSource instanceof DisposableBean) {
try {
((DisposableBean) myDataSource).destroy();
} catch (Exception theE) {
// ignore
}
}
} }
} }
} }