首页 > 技术文章 > 数据库编程加入transaction

s1127736971 2018-10-25 20:23 原文

TransManager tm = new TransManager();
    tm.begin();//开启事物
    try {
        //sql执行代码
        //
        //
         tm.commit();//更改完sql之后提交    
    }catch (Exception e){
        tm.rollback();//如果在提交一半的时候错误,就回滚。
    }
public static String printSybcbnryList() {// 外网

        TransManager tm = new TransManager();
        try {

            String sql = "select t.pid,t.aac002,t.sbsj from vw_xyzlrid t ";

            List addList = op.executeSQLQuery(sql);// 获取到了要更改的list

            for (int x = 0; x < addList.size(); x++) {
                tm.begin();
                try {
                    BasicDynaBean bdbean = (BasicDynaBean) addList.get(x);
                    String aac002 = bdbean.get("aac002").toString();
                    String pid = bdbean.get("pid").toString();
                    String sbsj = bdbean.get("sbsj").toString();
                    // 调用信用接口
                    String messageString = null;
                    Map<String, String> map = new LinkedHashMap<String, String>();
                    map.put("accessKey", "rs_jgpt_zwnw");
                    map.put("secretKey", "rs_jgpt_zwnw170911");
                    map.put("objectId", aac002);
                    String url = "http://172.18.1.150:9010/esb/esbproxy";// ESB核心地址
                    String user = "esb";
                    String pwd = "985984FA6B44CE2751075D37FF59EECD";
                    String sid = "gov.xm.wbjh.getPersonCreditBriefInfo"; // 自然人信用接口
                    try {
                        messageString = EsbUtils.sendReq(url, user, pwd, sid, map);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        throw new YLZCBPException("调动信用接口失败,请联系管理员!");
                    }
                    Map resultsetMap = YlzesbResponseUtils.parseResponse(messageString);
                    List dataList = (List) MapUtils.getObject(resultsetMap, "retrieve");
                    for (int j = 0; j < dataList.size(); j++) {
                        Map map1 = (Map) dataList.get(j);
                        Xy_zlrxyxx entity = new Xy_zlrxyxx();
                        if (StringUtils.isEmpty((String) map1.get("OBJECT_ID"))) {
                            entity.setPid(pid);
                            entity.setQuerytime(sbsj);
                            entity.setQueryresult("查无结果");
                            op.save(entity);
                            break;
                        }
                        BeanHelper.copyProperties(map1, entity);
                        entity.setPid(pid);
                        entity.setOBJECT_ID(aac002);
                        entity.setQuerytime(sbsj);
                        op.save(entity);
                    }

                    tm.commit();
                } catch (Exception e) {
                    log.info("错误", e);
                    tm.rollback();
                }

            }

            return "Success";

        } catch (Exception e) {
            return e.getMessage();
        }

    }

 

推荐阅读