请教大家  事务 + 批处理 怎么用?
executeBatch() 方法怎么用?

解决方案 »

  1.   

    conn.setAutoCommit(false);   
                  stmt   =   conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,   
                                                                          ResultSet.CONCUR_UPDATABLE);   
                  stmt.addBatch(sql);   
                  ...         //这地方加你要写的其他batch处理   
                  stmt.executeBatch();   
                  conn.setAutoCommit(true);   
      

  2.   

    下面是ibatis的批处理,跟其他的也差不多,希望能帮上你。      DynamicSqlMapDAO ddao=(DynamicSqlMapDAO)dao;
         SqlMapClient smc=ddao.getSqlMapClient();
         try {
         smc.startTransaction();//开始事务
    smc.startBatch();//开始批处理
    } catch (SQLException e) {
    throw new Exception("批处理开始错误!");
    }
    for(int i=0;i<list.size();i++){

    //操作数据库的代码 if((i+1)%300==0){//300条提交一次,太多会报错
              try {
         smc.executeBatch();//执行批处理
         smc.startBatch();//开始下一次批处理
         } catch (SQLException e) {
         throw new ApplicationException("批处理执行错误!");
         }
         } }      try {
    smc.executeBatch();//执行最后一次批处理
    smc.endTransaction();//提交局部事务
    } catch (SQLException e) {
    throw new Exception("批处理执行错误!");
    }finally{
    try {
    smc.endTransaction();//提交局部事务
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
      

  3.   

    try {
    conn.setAutoCommit(false);  PreparedStatement pstmt = conn.prepareStatement(sql);
    ......
    pstmt.addBatch();
    ....
    pstmt.executeBatch();  } catch(Exception e) {
    e.printStackTrace();
    } finally {
    conn.setAutoCommit(true);  
    }
      

  4.   

    事务 我给你一个spring2.0的事务配置给你
    事务代码<!-- spring事务控制 -->
    <!-- 定义一个事物管理器,需要注入sessionFactory -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 这个是spring2.0的配置方式,必须修改头文件,加入schema -->
    <tx:advice id="myadvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED" read-only="false"/>
    <tx:method name="save*" propagation="REQUIRED" read-only="false"/>
    <tx:method name="update*" propagation="REQUIRED" read-only="false"/>
    <tx:method name="*" propagation="REQUIRED" read-only="true"/>
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="mypointcut" expression="execution(* cn.xiaozejun.biz.imp.*.*(..))"/>
    <aop:advisor advice-ref="myadvice" pointcut-ref="mypointcut"/>
    </aop:config>配置事务的application.xml文件的文件头:<?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">批处理和其他方法差不多 不过他能够一次执行多条语句而已
      

  5.   

    gfhgfhdfhdfghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
      

  6.   

    stmt.executeBatch()  返回值的区别???
      

  7.   

    批处理就是:多个add,update,delte混合sql ,最后一起执行
      

  8.   

    try { 
    conn.setAutoCommit(false);  PreparedStatement pstmt = conn.prepareStatement(sql); 
    ...... 
    pstmt.addBatch(); 
    .... 
    pstmt.executeBatch(); 
    conn.commit(); 
    conn.setAutoCommit(true);} catch(SQLException e) {try{
    if(conn!=null)
    conn.rollback();
    conn.setAutoCommit(true);
    }catch(Exception ee){
    ee.printstack(); 
    e.printStackTrace(); 
    } finally { 
    try{
    if(ps!=null)
    ps.close(); 
    if(conn!=null)
    conn.close();
    catch(Exception e){
    System.out.println(e.getMessage);}
      

  9.   

    stmt.executeBatch()   这个方法 返回值有几种情况?分别表示什么状态?谢谢大家!
      

  10.   

    返回更新计数组成的数组,有下面3中情况:
    1 大于等于 0 的数 - 指示成功处理了命令,是给出执行命令所影响数据库中行数的更新计数 2 SUCCESS_NO_INFO 的值 - 指示成功执行了命令,但受影响的行数是未知的 
    如果批量更新中的命令之一无法正确执行,则此方法抛出 BatchUpdateException,并且 JDBC 驱动程序可能继续处理批处理中的剩余命令,也可能不执行。无论如何,驱动程序的行为必须与特定的 DBMS 一致,要么始终继续处理命令,要么永远不继续处理命令。如果驱动程序在某一次失败后继续进行处理,则 BatchUpdateException.getUpdateCounts 方法返回的数组将包含的元素与批中存在的命令一样多,并且其中至少有一个元素将为: 
    3 EXECUTE_FAILED 的值 - 指示未能成功执行命令,仅当命令失败后驱动程序继续处理命令时出现 
      

  11.   

    executeBatch() 这个方法怎么用? 谢谢!
      

  12.   

    其实我也想知道,
    pstmt.addBatch(); 
    .... 
    pstmt.executeBatch();  
    推测,是先将一些操作添加到批量里,就是上面的第一句,
    添加完成,然后执行.就是第二句?然后数据库执行批量操作(根本不用理它如何工作的)
    有个问题,这上面的好像是一条SQL,比如像这样 update tablename set columnA='XX'+'y' where columnb=condition; 这样的.
    如果是这样,那这些语句就是有相同的condition了,比如名字一样.或地址一样.或性别一样.
    如果我要执行多条SQL,比如我想更新某个列,完了再更新其它的.是不是和批量扯不上关系?