BATCH-1407: make samples work with MySQL.
This commit is contained in:
@@ -25,6 +25,7 @@ import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerDao;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.support.JdbcDaoSupport;
|
||||
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
@@ -33,9 +34,15 @@ import org.springframework.jdbc.core.support.JdbcDaoSupport;
|
||||
public class JdbcCustomerDao extends JdbcDaoSupport implements CustomerDao{
|
||||
|
||||
private static final String GET_CUSTOMER_BY_NAME = "SELECT ID, NAME, CREDIT from CUSTOMER where NAME = ?";
|
||||
private static final String INSERT_CUSTOMER = "INSERT into CUSTOMER(NAME, CREDIT) values(?,?)";
|
||||
private static final String INSERT_CUSTOMER = "INSERT into CUSTOMER(ID, NAME, CREDIT) values(?,?,?)";
|
||||
private static final String UPDATE_CUSTOMER = "UPDATE CUSTOMER set CREDIT = ? where NAME = ?";
|
||||
|
||||
private DataFieldMaxValueIncrementer incrementer;
|
||||
|
||||
public void setIncrementer(DataFieldMaxValueIncrementer incrementer) {
|
||||
this.incrementer = incrementer;
|
||||
}
|
||||
|
||||
public CustomerCredit getCustomerByName(String name) {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -64,7 +71,7 @@ public class JdbcCustomerDao extends JdbcDaoSupport implements CustomerDao{
|
||||
|
||||
public void insertCustomer(String name, BigDecimal credit) {
|
||||
|
||||
getJdbcTemplate().update(INSERT_CUSTOMER, new Object[]{name, credit});
|
||||
getJdbcTemplate().update(INSERT_CUSTOMER, new Object[]{incrementer.nextIntValue(), name, credit});
|
||||
}
|
||||
|
||||
public void updateCustomer(String name, BigDecimal credit) {
|
||||
|
||||
@@ -18,6 +18,7 @@ package test.jdbc.datasource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -27,85 +28,67 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
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;
|
||||
import org.springframework.util.ClassUtils;
|
||||
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/>
|
||||
*
|
||||
* 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
|
||||
* <code>hsql.server</code> project. Then you can right click in Eclipse and
|
||||
* Run As -> Java Application. Do the same any time you want to wipe the
|
||||
* database and start again.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DataSourceInitializer.class);
|
||||
|
||||
private Resource[] initScripts;
|
||||
|
||||
private Resource[] destroyScripts;
|
||||
private Resource destroyScript;
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private boolean ignoreFailedDrop = true;
|
||||
private boolean initialize = false;
|
||||
|
||||
private Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private static boolean initialized = false;
|
||||
|
||||
/**
|
||||
* Main method as convenient entry point.
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String... args) {
|
||||
new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(DataSourceInitializer.class,
|
||||
DataSourceInitializer.class.getSimpleName() + "-context.xml"));
|
||||
public void setInitialize(boolean initialize) {
|
||||
this.initialize = initialize;
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
if (destroyScripts==null) return;
|
||||
for (int i = 0; i < destroyScripts.length; i++) {
|
||||
Resource destroyScript = initScripts[i];
|
||||
try {
|
||||
public void destroy() throws Exception {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (destroyScript!=null) {
|
||||
doExecuteScript(destroyScript);
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.warn("Could not execute destroy script [" + destroyScript + "]", e);
|
||||
}
|
||||
else {
|
||||
logger.warn("Could not execute destroy script [" + destroyScript + "]");
|
||||
}
|
||||
initialized = false;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.warn("Could not execute destroy script [" + destroyScript + "]", e);
|
||||
}
|
||||
else {
|
||||
logger.warn("Could not execute destroy script [" + destroyScript + "]");
|
||||
}
|
||||
}
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(dataSource);
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initialize() {
|
||||
if (!initialized) {
|
||||
destroy();
|
||||
logger.info("Initializing with scripts: "+Arrays.asList(initScripts));
|
||||
if (!initialized && initialize) {
|
||||
try {
|
||||
doExecuteScript(destroyScript);
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.debug("Could not execute destroy script [" + destroyScript + "]", e);
|
||||
}
|
||||
if (initScripts != null) {
|
||||
for (int i = 0; i < initScripts.length; i++) {
|
||||
Resource initScript = initScripts[i];
|
||||
logger.info("Executing init script: "+initScript);
|
||||
doExecuteScript(initScript);
|
||||
}
|
||||
}
|
||||
@@ -133,17 +116,7 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||
for (int i = 0; i < scripts.length; i++) {
|
||||
String script = scripts[i].trim();
|
||||
if (StringUtils.hasText(script)) {
|
||||
try {
|
||||
jdbcTemplate.execute(script);
|
||||
}
|
||||
catch (DataAccessException e) {
|
||||
if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) {
|
||||
logger.debug("DROP script failed (ignoring): " + script);
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
jdbcTemplate.execute(scripts[i]);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -163,20 +136,20 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public Class<DataSource> getObjectType() {
|
||||
return DataSource.class;
|
||||
}
|
||||
|
||||
public void setInitScripts(Resource[] initScripts) {
|
||||
this.initScripts = initScripts;
|
||||
}
|
||||
|
||||
public void setDestroyScripts(Resource[] destroyScripts) {
|
||||
this.destroyScripts = destroyScripts;
|
||||
public void setDestroyScript(Resource destroyScript) {
|
||||
this.destroyScript = destroyScript;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public void setIgnoreFailedDrop(boolean ignoreFailedDrop) {
|
||||
this.ignoreFailedDrop = ignoreFailedDrop;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user