所以这是我的第一个问题,所以它就在这里......我的想法是我正在对数据库表执行大量更新语句。在 sql 中,这将是一个简单的
. 由于有数以百万计的这些,最好将其中一些一起批处理。我在这里遵循了指示:http:
//docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html
以及我在stackoverflow和其他地方随机找到的其他一些页面,但它们都读相似。我的想法是不进行读取,而是直接进行更新并像这样继续循环:
update table_name set col1 = 123 where col2 = 456 and col1 is null
sessionFactory = new Configuration().configure("finaldetail/hibernate.dev.cfg.xml")
.addPackage("com.xxx.app.ftm.domain.event").addAnnotatedClass(FinalTrainDetail.class)
.addAnnotatedClass(AbstractDetail.class).addAnnotatedClassFinalTrainDetailWork.class).buildSessionFactory();
inputStream = new BufferedReader(new FileReader(new File(args[0])));
session = sessionFactory.openSession();
transaction = session.beginTransaction();
String s;
int count = 0;
while ((s = inputStream.readLine()) != null) {
Query query = session.createQuery("update FinalTrainDetail detail set detail.trainSummary "
+ "=:summaryId where detail.trainDetail=:detailId and detail.trainSummary=null");
query.setParameter("summaryId", new Long(s.substring(9, 18)));
query.setParameter("detailId", new Long(s.substring(0, 9)));
query.executeUpdate();
count++;
if (count % 20 == 0) {
log.debug("should commit");
session.flush();
session.clear();
}
}
transaction.commit();
System.out.println("exit");
} catch (IOException e) {
transaction.rollback();
log.error(e.toString());
} catch (Throwable t) {
System.out.print(t);
log.error("exception caught during Updateing Offline", t);
System.exit(2);
} finally {
if (inputStream != null)
inputStream.close();
session.close();
}
所以这里的理解是flush会导致每20次更新commit,然后clear清空一级缓存以避免OutOfMemory异常。
到目前为止我有一个配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@dev264.oracle.XXXX.com:1521:DEV264</property>
<property name="connection.username">XXXX</property>
<property name="connection.password">XXX</property>
<property name="connection.shutdown">true</property>
<!-- JDBC connection pool (use the built-in one) -->
<property name="connection.pool_size">1</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- disable batching so HSQLDB will propagate errors correctly. -->
<property name="jdbc.batch_size">20</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
为调试目的启用了 Show sql。所以我没有得到或没有看到的是当我在 log4j 中设置时
<logger name="org.hibernate.transaction">
<level value="debug"/>
<appender-ref ref="file"/>
<appender-ref ref="console"/>
</logger>
我只看到
[DEBUG] [main] [org.hibernate.transaction.JDBCTransaction] [commit]
[DEBUG] [main] [org.hibernate.transaction.JDBCTransaction] [commit]
[DEBUG] [main] [org.hibernate.transaction.JDBCTransaction] [commit]
在日志文件的最后,并且在“刷新”发生时不会发生。所以我想知道的是,提交是否真的每 20 条记录被调用一次,以及我是否在内存中建立了太多的对象,并且当我没有数十万条测试记录运行时,我是否会在生产中获得 OutOfMemory .