Hibernate批處理

/*Transaction tran = session.beginTransaction();
        Connection connection = session.connection();
        String sql = "INSERT INTO DomainBandwidth(domain_id,traffic,time) VALUES(?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            for (SnFlowDetailResult snFlowDetailResult : snFlowDetailResults){
                boolean flag = false;
                if (flag){

                }else{
                    ps.setInt(1, domainId);
                    ps.setDouble(2, snFlowDetailResult.getTraffic());
                    ps.setTimestamp(3, new Timestamp(snFlowDetailResult.getTime()));
                    ps.addBatch();//添加到批次
                }
            }
            ps.executeBatch();
            tran.commit();
        } catch (Exception e) {
            tran.rollback();
            log.error("", e);
        }finally {
            try {
                ps.close();
                connection.close();
                session.close();
            } catch (Exception e) {
                log.error("", e);
            }
        }*/
//sql語句
//        String updateSql = "UPDATE DomainBandwidth SET traffic = ? WHERE domain_id = ? and time = ?";
        //通過session獲取事務 和spring整合之後這步並不需要
//        Transaction ts = session.beginTransaction();
        /*session.doWork(
                //實現work接口,work接口中有一個execute方法,connection就是這個方法的參數,也就是在這裏獲得jdbc的connection
                new Work(){
                    @Override
                    public void execute(java.sql.Connection connection) throws SQLException {
                        try {
                            connection = session.connection();
                            PreparedStatement stmt = connection.prepareStatement(updateSql);
                            //這裏進行批量操作
                            for (SnFlowDetailResult snFlowDetailResult : snFlowDetailResults){
                                stmt.setDouble(1, snFlowDetailResult.getTraffic());
                                stmt.setInt(2, domainId);
                                stmt.setTimestamp(3, new Timestamp(snFlowDetailResult.getTime()));
                                stmt.addBatch(); // 添加到批處理命令
                            }
                            stmt.executeBatch();
                            ts.commit();
                        } catch (Exception e) {
                            e.printStackTrace();
                            ts.rollback();
                            session.close();
                        }
                    }
                });
        session.close();*/

噹噹
https://blog.csdn.net/jinjiankang/article/details/78846518
https://gelei1014.iteye.com/blog/2095976
https://blog.csdn.net/zjt1388/article/details/52411901
http://www.imooc.com/article/277638
https://blog.csdn.net/yehuang_0801/article/details/78731122
https://www.cnblogs.com/codestory/p/5591651.html
https://blog.csdn.net/myshy025tiankong/article/details/83096720
http://www.manongjc.com/article/4243.html
https://blog.csdn.net/weixin_41704829/article/details/83023543

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章