IN PROGRESS - issue BATCH-792: Automatically detect database type in JobRepositoryFactoryBean
http://jira.springframework.org/browse/BATCH-792 JobRepositoryFactoryBean now detects the database type by the meta data. There is also now an enum representing the DatabaseType.
This commit is contained in:
@@ -18,6 +18,8 @@ package org.springframework.batch.core.repository.support;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
import org.springframework.aop.support.NameMatchMethodPointcut;
|
||||
@@ -33,6 +35,7 @@ import org.springframework.batch.core.repository.dao.JobInstanceDao;
|
||||
import org.springframework.batch.core.repository.dao.StepExecutionDao;
|
||||
import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory;
|
||||
import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory;
|
||||
import org.springframework.batch.support.DatabaseType;
|
||||
import org.springframework.batch.support.PropertiesConverter;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -54,6 +57,8 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean implements InitializingBean {
|
||||
|
||||
protected static final Log logger = LogFactory.getLog(JobRepositoryFactoryBean.class);
|
||||
|
||||
/**
|
||||
* Default value for isolation level in create* method.
|
||||
*/
|
||||
@@ -136,6 +141,11 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
|
||||
if (incrementerFactory == null) {
|
||||
incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
|
||||
}
|
||||
|
||||
if(databaseType == null){
|
||||
logger.info("No database type set, using meta data");
|
||||
databaseType = DatabaseType.fromMetaData(dataSource).name();
|
||||
}
|
||||
|
||||
Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType
|
||||
+ "' is an unsupported database type. The supported database types are "
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.batch.core.repository.support;
|
||||
|
||||
import static junit.framework.Assert.*;
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory;
|
||||
@@ -29,6 +30,7 @@ import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
@@ -63,19 +65,24 @@ public class JobRepositoryFactoryBeanTests {
|
||||
@Test
|
||||
public void testNoDatabaseType() throws Exception {
|
||||
|
||||
try {
|
||||
expect(incrementerFactory.isSupportedIncrementerType(null)).andReturn(false);
|
||||
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
|
||||
replay(incrementerFactory);
|
||||
factory.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
String message = ex.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.indexOf("unsupported database type") >= 0);
|
||||
}
|
||||
DatabaseMetaData dmd = createMock(DatabaseMetaData.class);
|
||||
Connection con = createMock(Connection.class);
|
||||
expect(dataSource.getConnection()).andReturn(con);
|
||||
expect(con.getMetaData()).andReturn(dmd);
|
||||
expect(dmd.getDatabaseProductName()).andReturn("Oracle");
|
||||
|
||||
expect(incrementerFactory.isSupportedIncrementerType("ORACLE")).andReturn(true);
|
||||
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
|
||||
expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_SEQ")).andReturn(new StubIncrementer());
|
||||
expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_EXECUTION_SEQ")).andReturn(new StubIncrementer());
|
||||
expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "STEP_EXECUTION_SEQ")).andReturn(new StubIncrementer());
|
||||
replay(dataSource,con,dmd, incrementerFactory);
|
||||
|
||||
factory.afterPropertiesSet();
|
||||
factory.getObject();
|
||||
|
||||
verify(incrementerFactory);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -102,6 +102,33 @@ public class StepExecutionResourceProxyTests extends TestCase {
|
||||
resource.beforeStep(jobExecution.createStepExecution(step.getName()));
|
||||
doTestPathName("spam-foo", "foo" + pathsep + "data" + pathsep);
|
||||
}
|
||||
|
||||
// public void testNonExistentJobParameter() throws Exception{
|
||||
//
|
||||
// resource.setFilePattern("foo/data/%JOB_NAME%/%non.key%-foo");
|
||||
// jobInstance = new JobInstance(new Long(0), new JobParametersBuilder().addString("job.key", "spam")
|
||||
// .toJobParameters(), "testJob");
|
||||
// JobExecution jobExecution = new JobExecution(jobInstance);
|
||||
// Step step = new StepSupport("bar");
|
||||
// try{
|
||||
// resource.beforeStep(jobExecution.createStepExecution(step.getName()));
|
||||
// fail();
|
||||
// }
|
||||
// catch(Exception ex){
|
||||
// //expected, if there isn't a JobParameter for that key, it should throw an exception
|
||||
// }
|
||||
// }
|
||||
|
||||
public void testLongJobParameter() throws Exception {
|
||||
|
||||
resource.setFilePattern("foo/data/%JOB_NAME%/%job.key(long)%-foo");
|
||||
jobInstance = new JobInstance(new Long(0), new JobParametersBuilder().addLong("job.key", 123L)
|
||||
.toJobParameters(), "testJob");
|
||||
JobExecution jobExecution = new JobExecution(jobInstance);
|
||||
Step step = new StepSupport("bar");
|
||||
resource.beforeStep(jobExecution.createStepExecution(step.getName()));
|
||||
doTestPathName("123-foo", "foo" + pathsep + "data" + pathsep);
|
||||
}
|
||||
|
||||
public void testResoureLoaderAware() throws Exception {
|
||||
resource = new StepExecutionResourceProxy();
|
||||
|
||||
Reference in New Issue
Block a user