`

spring管理 hibernate 事务冲突问题

阅读更多

废话不多说看看我的方法


     try {
  Session session = this.getHibernateSession();
     //Transaction tx = null;
  Query queryObject = null;
  String sql=null;
  //tx=session.beginTransaction();
  sql = "update tco_sta_info_service set protocol_version=?,service_name=?,data=?,is_can_mo_del=?,is_service_open=?,fee_type=?,fee_code=?,service_brief_cmd=?,service_brief=?,patch_id=?,sp_code=?,min_size=?,card_fun_type=?,CREATION_DATE=sysdate where id=?";
  queryObject = session.createSQLQuery(sql);
  queryObject.setString(0, srvInfo.getProtocolVersion()).setString(1, srvInfo.getServiceName())
  .setString(2, srvInfo.getData()).setString(3, srvInfo.getIsCanMoDel())
  .setString(4, srvInfo.getIsServiceOpen()).setString(5, srvInfo.getFeeType())
  .setInteger(6, srvInfo.getFeeCode()).setString(7, srvInfo.getSrvBriefCmd())
  .setString(8,  srvInfo.getSrvBrief()).setString(9, srvInfo.getPatchId())
  .setString(10, srvInfo.getSpCode()).setString(11, srvInfo.getMinSize())
  .setString(12, srvInfo.getCardFunType()).setInteger(13, srvInfo.getId());
   queryObject.executeUpdate();
   try {
   session.connection().commit();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    //session.getTransaction().commit();
   session.flush();
  
   return 1;
     } catch (HibernateException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return 0;
     }
         实现说明 --我这里的session 是通过spring 注入的 用hibernate 去执行 sql 了,自己在程序代码中写一套提交  事务,有报了异常 ,但是我又没写这个对象的映射文件,但又要提交事务,所以我用了   session.connection().commit();
 通过jdbc的连接对象关闭,不用transaction 这个hiberante 搞出的对象提交事务,就OK  了!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics