栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

高效使用多次准备语句

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

高效使用多次准备语句

您可以考虑

addBatch()
一次性使用和执行语句背面。另外,正如@pst在您的问题中所述,请考虑使用
trasaction

您将执行以下操作:

boolean autoCommit = connection.getAutoCommit();try{    connection.setAutoCommit(false //Turn off autoCommit    pstatement = db_connection.prepareStatement(PDSLnPConstants.UPSERT_SQL);    int batchLimit = 1000;    try{        for (Entry<Integer, linkedHashMap<Integer, String>> entry : MAPPING.entrySet()) { pstatement.setInt(1, entry.getKey()); pstatement.setString(2, entry.getValue().get(LnPConstants.CGUID_ID)); pstatement.setString(3, entry.getValue().get(LnPConstants.PGUID_ID)); pstatement.setString(4, entry.getValue().get(LnPConstants.SGUID_ID)); pstatement.setString(5, entry.getValue().get(LnPConstants.UID_ID)); pstatement.setString(6, entry.getValue().get(LnPConstants.ULOC_ID)); pstatement.setString(7, entry.getValue().get(LnPConstants.SLOC_ID)); pstatement.setString(8, entry.getValue().get(LnPConstants.PLOC_ID)); pstatement.setString(9, entry.getValue().get(LnPConstants.ALOC_ID)); pstatement.setString(10, entry.getValue().get(LnPConstants.SITE_ID)); pstatement.addBatch(); batchLimit--; if(batchLimit == 0){     pstatement.executeBatch();     pstatement.clearBatch     batchLimit = 1000; }  pstatement.clearParameters();        }    }finally{        //for the remaining ones        pstatement.executeBatch();        //commit your updates        connection.commit();    }}finally{    connection.setAutoCommit(autoCommit);}

这个想法是为批处理更新设置一个限制,并且仅在达到特定限制时才执行数据库更新。这样,您就可以将数据库调用限制为每次

batchLimit
定义一次。这样会更快。

另请注意

transaction
,我已经展示了如何以及何时使用
commit
。这可能并不总是正确的指向,
commit
因为此决定将基于您的要求。您可能还想
rollback
在发生异常的情况下执行。因此,由您决定。

查看“使用交易”教程,以更好地了解如何使用

transaction



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438585.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号