BATCH-1407: make samples work with MySQL.

This commit is contained in:
dsyer
2009-09-22 11:33:21 +00:00
parent 7f11e1ef58
commit 78768959b0
27 changed files with 148 additions and 185 deletions

View File

@@ -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) {

View File

@@ -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 -&gt; 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;
}
}

View File

@@ -7,16 +7,11 @@ batch.jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true
# batch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples
batch.jdbc.user=sa
batch.jdbc.password=
batch.schema=
batch.jndi.name=
batch.naming.factory.initial=
batch.naming.provider.url=
batch.schema.script=schema-hsqldb.sql
batch.drop.script=schema-drop-hsqldb.sql
batch.business.schema.script=business-schema-hsqldb.sql
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.schema.script=classpath:/schema-hsqldb.sql
batch.business.schema.script=classpath:/business-schema-hsqldb.sql
batch.data.source.init=true
batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer
batch.lob.handler.class=org.springframework.jdbc.support.lob.DefaultLobHandler
# Bean Properties for override
# when not using sequences:
incrementerParent.columnName=ID
batch.database.incrementer.parent=columnIncrementerParent
batch.grid.size=2

View File

@@ -2,18 +2,14 @@
# for MySQL:
batch.jdbc.driver=com.mysql.jdbc.Driver
batch.jdbc.url=jdbc:mysql://localhost/test
batch.jdbc.user=root
batch.jdbc.password=root
batch.schema=
batch.jndi.name=
batch.naming.factory.initial=
batch.naming.provider.url=
batch.schema.script=schema-mysql.sql
batch.drop.script=schema-drop-mysql.sql
batch.business.schema.script=business-schema-mysql.sql
batch.jdbc.user=test
batch.jdbc.password=test
batch.jdbc.testWhileIdle=true
batch.jdbc.validationQuery=SELECT 1
batch.drop.script=classpath:schema-drop-mysql.sql
batch.schema.script=classpath:schema-mysql.sql
batch.business.schema.script=classpath:business-schema-mysql.sql
batch.data.source.init=true
batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer
batch.lob.handler.class=org.springframework.jdbc.support.lob.DefaultLobHandler
# Bean Properties for override
# when not using sequences:
incrementerParent.columnName=ID
batch.database.incrementer.parent=columnIncrementerParent
batch.grid.size=50

View File

@@ -13,8 +13,11 @@ DROP TABLE ERROR_LOG ;
-- Autogenerated: do not edit this file
CREATE TABLE CUSTOMER_SEQ (ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, DUMMY VARCHAR(1));
INSERT INTO CUSTOMER_SEQ (ID) values (4);
CREATE TABLE BATCH_STAGING_SEQ (ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, DUMMY VARCHAR(1));
INSERT INTO BATCH_STAGING_SEQ (ID) values (0);
CREATE TABLE TRADE_SEQ (ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, DUMMY VARCHAR(1));
INSERT INTO TRADE_SEQ (ID) values (0);
CREATE TABLE BATCH_STAGING (
ID BIGINT NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,

View File

@@ -15,12 +15,15 @@ DROP TABLE ERROR_LOG IF EXISTS;
CREATE TABLE CUSTOMER_SEQ (
ID BIGINT IDENTITY
);
INSERT INTO CUSTOMER_SEQ (ID) values (4);
CREATE TABLE BATCH_STAGING_SEQ (
ID BIGINT IDENTITY
);
INSERT INTO BATCH_STAGING_SEQ (ID) values (0);
CREATE TABLE TRADE_SEQ (
ID BIGINT IDENTITY
);
INSERT INTO TRADE_SEQ (ID) values (0);
CREATE TABLE BATCH_STAGING (
ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,

View File

@@ -13,7 +13,7 @@ DROP TABLE IF EXISTS ERROR_LOG ;
-- Autogenerated: do not edit this file
CREATE TABLE CUSTOMER_SEQ (ID BIGINT NOT NULL) type=MYISAM;
INSERT INTO CUSTOMER_SEQ values(0);
INSERT INTO CUSTOMER_SEQ values(4);
CREATE TABLE BATCH_STAGING_SEQ (ID BIGINT NOT NULL) type=MYISAM;
INSERT INTO BATCH_STAGING_SEQ values(0);
CREATE TABLE TRADE_SEQ (ID BIGINT NOT NULL) type=MYISAM;

View File

@@ -13,8 +13,11 @@ DROP TABLE ERROR_LOG ;
-- Autogenerated: do not edit this file
CREATE TABLE CUSTOMER_SEQ (ID BIGINT IDENTITY);
INSERT INTO CUSTOMER_SEQ (ID) values (4);
CREATE TABLE BATCH_STAGING_SEQ (ID BIGINT IDENTITY);
INSERT INTO BATCH_STAGING_SEQ (ID) values (0);
CREATE TABLE TRADE_SEQ (ID BIGINT IDENTITY);
INSERT INTO TRADE_SEQ (ID) values (0);
CREATE TABLE BATCH_STAGING (
ID BIGINT NOT NULL PRIMARY KEY ,

View File

@@ -13,8 +13,11 @@ DROP TABLE ERROR_LOG ;
-- Autogenerated: do not edit this file
CREATE TABLE CUSTOMER_SEQ (ID BIGINT IDENTITY);
INSERT INTO CUSTOMER_SEQ (ID) values (4);
CREATE TABLE BATCH_STAGING_SEQ (ID BIGINT IDENTITY);
INSERT INTO BATCH_STAGING_SEQ (ID) values (0);
CREATE TABLE TRADE_SEQ (ID BIGINT IDENTITY);
INSERT INTO TRADE_SEQ (ID) values (0);
CREATE TABLE BATCH_STAGING (
ID BIGINT NOT NULL PRIMARY KEY ,

View File

@@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSourceInitializer" class="test.jdbc.datasource.DataSourceInitializer">
<property name="dataSource" ref="dataSource"/>
<property name="initScripts">
<list>
<value>${batch.drop.script}</value>
<value>${batch.schema.script}</value>
<value>${batch.business.schema.script}</value>
</list>
</property>
</bean>
</beans>

View File

@@ -5,7 +5,17 @@
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<!-- Initialise the database before every test case: -->
<import resource="data-source-context-init.xml" />
<bean id="dataSourceInitializer" class="test.jdbc.datasource.DataSourceInitializer">
<property name="dataSource" ref="dataSource"/>
<property name="initialize" value="${batch.data.source.init}"/>
<property name="initScripts">
<list>
<value>${batch.drop.script}</value>
<value>${batch.schema.script}</value>
<value>${batch.business.schema.script}</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${batch.jdbc.driver}" />
@@ -45,11 +55,13 @@
<property name="order" value="1" />
</bean>
<bean id="lobHandler" class="${batch.lob.handler.class}" />
<bean id="incrementerParent" class="${batch.database.incrementer.class}">
<bean id="columnIncrementerParent" class="${batch.database.incrementer.class}" abstract="true">
<property name="dataSource" ref="dataSource" />
<property name="incrementerName" value="ID" />
<property name="columnName" value="ID" />
</bean>
<bean id="incrementerParent" parent="${batch.database.incrementer.parent}">
<property name="incrementerName" value="DUMMY" />
</bean>
<!--import resource="alt-data-source-context.xml" /-->

View File

@@ -24,7 +24,7 @@
<bean name="customerFileUploadReader"
class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" ref="customerFileResource" />
<property name="resource" value="classpath:data/customerFilterJob/input/customers.txt" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer" ref="customerTokenizer" />
@@ -66,10 +66,11 @@
<bean name="customerDao"
class="org.springframework.batch.sample.domain.trade.internal.JdbcCustomerDao">
<property name="dataSource" ref="dataSource" />
</bean>
<bean name="customerFileResource" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="data/customerFilterJob/input/customers.txt" />
<property name="incrementer">
<bean parent="${batch.database.incrementer.parent}">
<property name="incrementerName" value="CUSTOMER_SEQ"/>
</bean>
</property>
</bean>
</beans>

View File

@@ -1,25 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<import resource="classpath:data-source-context.xml" />
<bean id="processor"
class="org.springframework.batch.sample.common.StagingItemWriter">
<bean id="processor" class="org.springframework.batch.sample.common.StagingItemWriter">
<property name="incrementer">
<bean id="jobIncrementer" parent="incrementerParent">
<property name="incrementerName"
value="BATCH_STAGING_SEQ" />
<bean parent="${batch.database.incrementer.parent}">
<property name="incrementerName" value="BATCH_STAGING_SEQ" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="provider"
class="org.springframework.batch.sample.common.StagingItemReader">
<bean id="provider" class="org.springframework.batch.sample.common.StagingItemReader">
<property name="dataSource" ref="dataSource" />
</bean>

View File

@@ -1,2 +1,2 @@
#macro (sequence $name)CREATE SEQUENCE ${name};
#macro (sequence $name $value)CREATE SEQUENCE ${name};
#end

View File

@@ -1,2 +1,3 @@
#macro (sequence $name)CREATE TABLE ${name} (ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, DUMMY VARCHAR(1));
#macro (sequence $name $value)CREATE TABLE ${name} (ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, DUMMY VARCHAR(1));
INSERT INTO ${name} (ID) values (${value});
#end

View File

@@ -1,4 +1,5 @@
#macro (sequence $name)CREATE TABLE ${name} (
#macro (sequence $name $value)CREATE TABLE ${name} (
ID BIGINT IDENTITY
);
INSERT INTO ${name} (ID) values (${value});
#end

View File

@@ -1,8 +1,8 @@
-- Autogenerated: do not edit this file
#sequence( "CUSTOMER_SEQ" )
#sequence( "BATCH_STAGING_SEQ" )
#sequence( "TRADE_SEQ" )
#sequence( "CUSTOMER_SEQ" 4 )
#sequence( "BATCH_STAGING_SEQ" 0 )
#sequence( "TRADE_SEQ" 0 )
CREATE TABLE BATCH_STAGING (
ID ${BIGINT} $!{IDENTITY} NOT NULL PRIMARY KEY $!{GENERATED},

View File

@@ -1,3 +1,3 @@
#macro (sequence $name)CREATE TABLE ${name} (ID BIGINT NOT NULL) type=MYISAM;
INSERT INTO ${name} values(0);
#macro (sequence $name $value)CREATE TABLE ${name} (ID BIGINT NOT NULL) type=MYISAM;
INSERT INTO ${name} values(${value});
#end

View File

@@ -1,2 +1,2 @@
#macro (sequence $name)CREATE SEQUENCE ${name};
#macro (sequence $name $value)CREATE SEQUENCE ${name};
#end

View File

@@ -1,2 +1,2 @@
#macro (sequence $name)CREATE SEQUENCE ${name};
#macro (sequence $name $value)CREATE SEQUENCE ${name};
#end

View File

@@ -1,3 +1,4 @@
#macro (sequence $name)CREATE TABLE ${name} (ID BIGINT IDENTITY);
#macro (sequence $name $value)CREATE TABLE ${name} (ID BIGINT IDENTITY);
INSERT INTO ${name} (ID) values (${value});
#end
#macro (notnull $name $type)ALTER COLUMN ${name} ${type} NOT NULL#end

View File

@@ -1,3 +1,4 @@
#macro (sequence $name)CREATE TABLE ${name} (ID BIGINT IDENTITY);
#macro (sequence $name $value)CREATE TABLE ${name} (ID BIGINT IDENTITY);
INSERT INTO ${name} (ID) values (${value});
#end
#macro (notnull $name $type)ALTER COLUMN ${name} ${type} NOT NULL#end

View File

@@ -40,7 +40,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
@ContextConfiguration
public class CustomerFilterJobFunctionalTests extends AbstractJobTests {
private static final String GET_CUSTOMERS = "select NAME, CREDIT from CUSTOMER order by NAME";

View File

@@ -35,7 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
@ContextConfiguration
public class StagingItemWriterTests {
private SimpleJdbcTemplate simpleJdbcTemplate;