Polish 305b4b8b03
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
* Copyright 2006-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,8 +25,6 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -40,7 +38,6 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -49,7 +46,7 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Wrapper for a {@link DataSource} that can run scripts on start up and shut
|
||||
* down. Us as a bean definition <br><br>
|
||||
* down. Use as a bean definition <br>
|
||||
*
|
||||
* Run this class to initialize a database in a running server process.
|
||||
* Make sure the server is running first by launching the "hsql-server" from the
|
||||
@@ -58,7 +55,9 @@ import org.springframework.util.StringUtils;
|
||||
* database and start again.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @author Drummond Dawson
|
||||
* @author Mahmoud Ben Hassine
|
||||
*
|
||||
*/
|
||||
public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||
|
||||
@@ -85,11 +84,12 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||
DataSourceInitializer.class.getSimpleName() + "-context.xml"));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public void destroy() {
|
||||
if (destroyScripts==null) return;
|
||||
for (int i = 0; i < destroyScripts.length; i++) {
|
||||
Resource destroyScript = destroyScripts[i];
|
||||
if (this.destroyScripts == null) {
|
||||
return;
|
||||
}
|
||||
for (Resource destroyScript : this.destroyScripts) {
|
||||
try {
|
||||
doExecuteScript(destroyScript);
|
||||
}
|
||||
@@ -104,61 +104,56 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(dataSource, "A DataSource is required");
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
Assert.notNull(this.dataSource, "A DataSource is required");
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initialize() {
|
||||
if (!initialized) {
|
||||
if (!this.initialized) {
|
||||
destroy();
|
||||
if (initScripts != null) {
|
||||
for (int i = 0; i < initScripts.length; i++) {
|
||||
Resource initScript = initScripts[i];
|
||||
if (this.initScripts != null) {
|
||||
for (Resource initScript : this.initScripts) {
|
||||
doExecuteScript(initScript);
|
||||
}
|
||||
}
|
||||
initialized = true;
|
||||
this.initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void doExecuteScript(final Resource scriptResource) {
|
||||
if (scriptResource == null || !scriptResource.exists())
|
||||
if (scriptResource == null || !scriptResource.exists()) {
|
||||
return;
|
||||
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
String[] scripts;
|
||||
try {
|
||||
scripts = StringUtils
|
||||
.delimitedListToStringArray(stripComments(getScriptLines(scriptResource)), ";");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
|
||||
}
|
||||
for (int i = 0; i < scripts.length; i++) {
|
||||
String script = scripts[i].trim();
|
||||
if (StringUtils.hasText(script)) {
|
||||
try {
|
||||
jdbcTemplate.execute(script);
|
||||
}
|
||||
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(this.dataSource));
|
||||
transactionTemplate.execute((TransactionCallback<Void>) status -> {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
|
||||
String[] scripts;
|
||||
try {
|
||||
scripts = StringUtils
|
||||
.delimitedListToStringArray(stripComments(getScriptLines(scriptResource)), ";");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
|
||||
}
|
||||
for (String script : scripts) {
|
||||
String trimmedScript = script.trim();
|
||||
if (StringUtils.hasText(trimmedScript)) {
|
||||
try {
|
||||
jdbcTemplate.execute(trimmedScript);
|
||||
}
|
||||
catch (DataAccessException e) {
|
||||
if (this.ignoreFailedDrop && trimmedScript.toLowerCase().startsWith("drop")) {
|
||||
logger.debug("DROP script failed (ignoring): " + trimmedScript);
|
||||
}
|
||||
catch (DataAccessException e) {
|
||||
if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) {
|
||||
logger.debug("DROP script failed (ignoring): " + script);
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user