我在Spring Batch POC中检索自动生成的Oracle表列时遇到了挑战。
此POC从平面文件中读取数据并加载到oracle表中。我正在尝试使用Oracle Sequence为表生成主键,并在从Spring Batch Writer实现成功插入行后检索它。但它引发了以下错误。
org.springframework.jdbc.UncategorizedSQLException:
PreparedStatementCallback; uncategorized SQLException for SQL
我不确定我遗漏了任何与oracle驱动程序相关的问题。需要你宝贵的意见。
规格:
Oracle Database version 11.1.0.7
Oracle Driver that is added to Maven Proj ojdbc14-10.1.0.5.jar
JDK Version 1.8
spring.version 4.0.5.RELEASE
spring.jdbc.version 4.0.5
spring.batch.version 3.0.4.RELEASE
我的job.xml
<bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
我的CustomerItemWriter类:
public class CustomItemWriter implements ItemWriter<Permission>
{
private final static String INSERT_SQL = "insert into CUSTOMERS (CUST_ID,FIRSTNAME,LASTNAME,CREATED_DATE) VALUES (CUST_ID_SEQ.NEXTVAL,:firstname,:lastname,:CreatedDate)";
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Override
public void write(List<? extends Customer> customers) throws Exception
{
for( Customer customer : customers )
{
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("firstname", customer.getLdapUserId());
namedParameters.addValue("lastname", customer.getRoleId());
namedParameters.addValue("CreatedDate", customer.getCreatedDate());
final KeyHolder holder = new GeneratedKeyHolder();
this.namedParameterJdbcTemplate.update( INSERT_SQL, namedParameters, holder, new String[] {"CUST_ID" } );
Number generatedId = holder.getKey();
System.out.println("Oracle Seq value " + generatedId.intValue());
}
}
执行时收到异常:
Aug 24, 2016 12:02:56 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@728938a9: startup date [Wed Aug 24 00:02:56 EDT 2016]; root of context hierarchy
Aug 24, 2016 12:02:56 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/spring/job-config.xml]
Aug 24, 2016 12:02:56 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/spring/context-config.xml]
Aug 24, 2016 12:02:56 AM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'customReader': replacing [Generic bean: class [org.springframework.batch.item.file.FlatFileItemReader]; scope=step; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [META-INF/spring/context-config.xml]] with [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in class path resource [META-INF/spring/context-config.xml]]
Aug 24, 2016 12:02:56 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: oracle.jdbc.xa.client.OracleXADataSource
Aug 24, 2016 12:02:57 AM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
Aug 24, 2016 12:02:57 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: oracle.jdbc.xa.client.OracleXADataSource
Aug 24, 2016 12:03:00 AM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=GCMPBatchFeedJob]] launched with the following parameters: [{date=1472011377348, inputFile=C:/Work/BatchPOC/springbatch/GCMPBatchFeed/target/GCMPFeedData1.csv}]
Aug 24, 2016 12:03:06 AM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [importFileStep]
Aug 24, 2016 12:03:09 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
Aug 24, 2016 12:03:09 AM org.springframework.jdbc.support.SQLErrorCodesFactory <init>
INFO: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
Aug 24, 2016 12:03:09 AM org.springframework.batch.core.step.AbstractStep execute
SEVERE: Encountered an error executing step importFileStep in job GCMPBatchFeedJob
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO CUSTOMERS (CUST_ID,FIRSTNAME,LASTNAME,CREATED_DATE) VALUES (CUST_ID_SEQ.NEXTVAL, ?, ?, ?)]; SQL state [null]; error code [17023]; Unsupported feature; nested exception is java.sql.SQLException: Unsupported feature
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:943)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:344)
at com.comp.sbatch.writer.CustomItemWriter.write(CustomItemWriter.java:171)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:165)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
at com.comp.sbatch.App.main(App.java:35)
Caused by: java.sql.SQLException: Unsupported feature
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)
at oracle.jdbc.driver.DatabaseError.throwUnsupportedFeatureSqlException(DatabaseError.java:537)
at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:3094)
at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:233)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:638)
... 29 more
答案 0 :(得分:0)
您需要切换到适合您的数据库版本的较新驱动程序。在您的情况下,它应该是ojdbc6.jar
(但不是ojdbc7.jar
,它与Oracle 12一起使用并且与Oracle 11有问题。)