for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //20,与JDBC批量设置相同 //flush a batch of inserts and release memory: //将本批插入的对象立即写入数据库并释放内存 session.flush(); session.clear(); } }
楼主【meiqianmeifang】截止到2008-08-04 02:16:44的历史汇总数据(不包括此帖):
发帖的总数量:60 发帖的总分数:1140 每贴平均分数:19
回帖的总数量:35 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:39 结贴的总分数:720
无满意结贴数:2 无满意结贴分:40
未结的帖子数:21 未结的总分数:420
结贴的百分比:65.00 % 结分的百分比:63.16 %
无满意结贴率:5.13 % 无满意结分率:5.56 %
楼主加油
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=meiqianmeifang
13.1. 批量插入(Batch inserts)
如果要将很多对象持久化,你必须通过经常的调用 flush() 以及稍后调用 clear() 来控制第一级缓存的大小。 Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size //20,与JDBC批量设置相同
//flush a batch of inserts and release memory:
//将本批插入的对象立即写入数据库并释放内存
session.flush();
session.clear();
}
}
tx.commit();
session.close();13.2. 批量更新(Batch updates)
此方法同样适用于检索和更新数据。此外,在进行会返回很多行数据的查询时, 你需要使用 scroll() 方法以便充分利用服务器端游标所带来的好处。 Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
ScrollableResults customers = session.getNamedQuery("GetCustomers")
.setCacheMode(CacheMode.IGNORE)
.scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while ( customers.next() ) {
Customer customer = (Customer) customers.get(0);
customer.updateStuff(...);
if ( ++count % 20 == 0 ) {
//flush a batch of updates and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
要即使的把数据刷入到数据库
Transaction transaction = null;
ConnDB connDB = new ConnDB ();
try {
sessions = connDB.getSession ();
transaction = sessions.beginTransaction (); String sqlstr = "select cc from com.greatking.ajcrm.hibernate.CustomerCaring as cc where cc.customercaringid ="
+ Integer.parseInt (ccid);
Query sqlquery = sessions.createQuery (sqlstr);
List sqllist = (List) sqlquery.list (); if (!sqllist.isEmpty ()) {
CustomerCaring CC = (CustomerCaring) sqllist.get (0); CC.setCcustomercaringtitle (ccustomercaringtitle);
CC.setUpdatedate (Calendar.getInstance ().getTime ()); } sessions.flush ();
transaction.commit ();这是STRUTS的更新,如果批量需参考分批更新,以免过多问题.比如
if ( ++count % 20 == 0 ) {
//flush a batch of updates and release memory:
session.flush();
session.clear();
}
这样可以分批提交来增加效率