开发者社区> 问答> 正文

使用MyBaits3的ExecutorType.BATCH批量插入数据到oracle报错?报错

使用MyBaits3的ExecutorType.BATCH批量插入数据到oracle报错

一条条插入不报错,使用ExecutorType.SIMPLE批量也不报错。怎么回事呢?

ExecutorType.BATCH不能用吗??

org.apache.ibatis.exceptions.PersistenceException:

### Error committing transaction.  Cause: java.lang.ArrayIndexOutOfBoundsException
### Cause: java.lang.ArrayIndexOutOfBoundsException
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:170)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:162)
    at com.boco.writerfm.AppTest.testMyBatis(AppTest.java:344)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at oracle.jdbc.driver.T4CRowidAccessor.unmarshalOneRow(T4CRowidAccessor.java:202)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:610)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:213)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:952)
    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10580)
    at org.apache.ibatis.executor.BatchExecutor.doFlushStatements(BatchExecutor.java:99)
    at org.apache.ibatis.executor.BaseExecutor.flushStatements(BaseExecutor.java:117)
    at org.apache.ibatis.executor.BaseExecutor.flushStatements(BaseExecutor.java:112)
    at org.apache.ibatis.executor.BaseExecutor.commit(BaseExecutor.java:201)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:167)
    ... 21 more

展开
收起
爱吃鱼的程序员 2020-06-22 14:36:25 1448 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    是不是记录条数太多溢出了,把自动提交事务关闭,自己控制多少记录一起提交吧

    可以肯定不是条数的问题

    只insert2条,关闭了自动commit,每次2条insert后就commit。每次一条commit不报错!


    我也遇到了这个问题。有没有大牛知道要怎么解决啊。我的代码是这样的:

    <preclass="brush:java;toolbar:true;auto-links:false;">publicintinsertRowSubInfoList(List<IRowSubInfoModel>rowList)throwsSQLException{assertrowList!=null&&!rowList.isEmpty();//从当前环境中根据connection生成批量提交的sqlSessionSqlSessionsession=sqlSessionFactory.openSession(ExecutorType.BATCH,false);//原定手动管理事务,改为自动管理。//管理session默认的自动提交,以便于手动管理数据库事务。//如果不关闭,则session.flushStatements();时就会提交一次事务,从而破坏事务的原子性。session.getConnection().setAutoCommit(false);//事务工厂。这个东西可能是多线程安全的//TransactionFactorytransactionFactory=newJdbcTransactionFactory();//当前操作的事务。创建的同时就已经打开了。//TransactionnewTransaction=//transactionFactory.newTransaction(session//.getConnection());IRowSubInfoMappermapper=session.getMapper(IRowSubInfoMapper.class);////整个列表的长度,也是一次批量插入的总数据量。//intsize=rowList.size();try{//准备批量插入for(IRowSubInfoModelrow:rowList){//session.insert(BATCH_STATEMENT_INSERT,row);mapper.insertRowSubInfo(row);}session.commit(true);//如果上述操作全部成功完成,那么提交。//newTransaction.commit();}catch(Exceptione){//抛出任何异常,都回滚//但是,由于session.flushStatements();时会自动提交一次,因此,这里回滚的实际只有插入失败以后的数据。//已经插入数据库的数据,不会在此回滚session.rollback(true);//并且,将异常封装后,再次抛出,以通知调用者。thrownewSQLException(e);}finally{//最后关闭资源。//newTransaction.close();session.close();}returnrowList.size();}



    2020-06-22 14:36:42
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
PostgresChina2018_樊文凯_ORACLE数据库和应用异构迁移最佳实践 立即下载
PostgresChina2018_王帅_从Oracle到PostgreSQL的数据迁移 立即下载
Oracle云上最佳实践 立即下载

相关镜像