无法在春季批处理中序列化对此事务的访问

时间:2018-08-27 20:37:56

标签: spring-mvc spring-boot spring-batch

我正在将Spring Batch与Spring Boot配合使用来处理Csv文件。当我运行该应用程序时,出现以下错误跟踪。

2018-08-27 16:23:35.694  INFO 12016 --- [nio-9004-exec-1] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: ORACLE
2018-08-27 16:23:36.025  INFO 12016 --- [nio-9004-exec-1] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2018-08-27 16:23:36.898  INFO 12016 --- [nio-9004-exec-1] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2018-08-27 16:23:36.965  INFO 12016 --- [nio-9004-exec-1] o.s.jdbc.support.SQLErrorCodesFactory    : SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
2018-08-27 16:23:37.005 ERROR 12016 --- [nio-9004-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.CannotSerializeTransactionException: PreparedStatementCallback; SQL [INSERT INTO BATCH_JOB_EXECUTION_CONTEXT (SHORT_CONTEXT, SERIALIZED_CONTEXT, JOB_EXECUTION_ID) VALUES(?, ?, ?)]; ORA-08177: can't serialize access for this transaction
; nested exception is java.sql.SQLException: ORA-08177: can't serialize access for this transaction
] with root cause

java.sql.SQLException: ORA-08177: can't serialize access for this transaction

        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450) ~[ojdbc7-12.1.0.1.jar!/:12.1.0.1.0]
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399) ~[ojdbc7-12.1.0.1.jar!/:12.1.0.1.0]
        at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017) ~[ojdbc7-12.1.0.1.jar!/:12.1.0.1.0]
        at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655) ~[ojdbc7-12.1.0.1.jar!/:12.1.0.1.0]
        at oracle.jdbc.driver.T4CTTI

在Google中进行一些搜索后,我发现我们必须实例化 JobRepositoryFactoryBean 。所以我在BatchConfiguration中做了如下操作。

@Autowired
    private DataSource dataSource;
@Autowired
    private DataSourceTransactionManager transactionManager;   

 @Bean
        public JobRepositoryFactoryBean jobRepository() throws SQLException{
            JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
            factoryBean.setDatabaseType("ORACLE");
            factoryBean.setDataSource(dataSource);
            factoryBean.setTransactionManager(transactionManager);
            factoryBean.setIsolationLevelForCreate("ISOLATION_READ_UNCOMMITTED");
            return factoryBean;

        }

数据源**和** TransactionManager Bean在我的DBConfiguration中,如下所示。

@Bean
        public DataSource dataSource() throws SQLException
        {
            OracleDataSource dataSource = new OracleDataSource();
            dataSource.setUser("xxxxxx");
            dataSource.setPassword("xxxxx");
            dataSource.setURL("xxxxx");
            dataSource.setImplicitCachingEnabled(true);
            dataSource.setFastConnectionFailoverEnabled(true);



            return dataSource;

        }

@Bean(name="transactionManager")
        public PlatformTransactionManager  transactionManager() throws SQLException{

            DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.dataSource());
            return transactionManager;

        }

然后我遇到了错误

***************************
APPLICATION FAILED TO START
***************************

Description:

Field transactionManager in com.comcast.FileProcess.configuration.SprintgBatchConfiguration required a bean of type 'org.springframework.jdbc.datasource.DataSourceTransactionManager' that could not be found.
        - Bean method 'transactionManager' in 'DataSourceTransactionManagerAutoConfiguration.DataSourceTransactionManagerConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.transaction.PlatformTransactionManager; SearchStrategy: all) found bean 'transactionManager'


Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.jdbc.datasource.DataSourceTransactionManager' in your configuration.

任何人都可以帮助我解决此问题并运行我的批处理过程。

1 个答案:

答案 0 :(得分:0)

关于APPLICATION FAILED TO START错误,您需要使您的事务管理器bean定义方法返回实际类型DataSourceTransactionManager而不是PlatformTransactionManager

@Bean(name="transactionManager")
    public DataSourceTransactionManager  transactionManager() throws SQLException{

        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.dataSource());
        return transactionManager;

    }

关于java.sql.SQLException: ORA-08177: can't serialize access for this transaction错误,看来IsolationLevelForCreate仍然是ISOLATION_SERIALIZABLE,并且您的数据库无法序列化事务。这可能是由于未考虑您的作业存储库。我将使jobRepository方法返回作业存储库本身(如示例here中)而不返回工厂bean:

@Bean
    public JobRepository jobRepository() throws SQLException{
        JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
        factoryBean.setDatabaseType("ORACLE");
        factoryBean.setDataSource(dataSource);
        factoryBean.setTransactionManager(transactionManager);
        factoryBean.setIsolationLevelForCreate("ISOLATION_READ_UNCOMMITTED");
        return factoryBean.getObject();
    }

这样,您的作业存储库将使用ISOLATION_READ_UNCOMMITTED进行正确配置,并且数据库将不会尝试序列化事务。