开发者

Regarding hibernate caching

Here is the scenario:

I will select some records from my database, say 1000 rec开发者_如何学运维ords. Then I will do some operations on that result set, like insert one more record in it or update an existing record.

The entire above mentioned process will repeat multiple times, say 2000 times. (i.e. I will again select some records, but they need not be exactly the same records every time)

I am sure that I can use some sort of caching for better performance. But I don't know how. Can someone guide me?


When you use Hibernate, you are actually using two caches. The first is the session cache. Let's say you query 2000 records, update and save changes to 5, and then run the same query again (all in the scope of a single session). Hibernate will not actually run the query a second time - it knows that you have the 2000 records (and the 5 edits) already loaded into memory. This cache is turned on automatically - you can't turn it off, as it's part of Hibernate's core functionality. You do need to close (or at least flush) your session to actually ensure that changes are applied - you don't just want to open one giant session and keep changing things or eventually you will run out of memory.

The second level cache basically puts a key-value store in between your application and the database. This cache is generally longer-lived, and multiple sessions can use it, but it's also more complex (e.g. needs to properly deal with threading, invalidation, etc). The biggest problem is that if your data is changing a lot, you are actually have to make changes both in the second level cache and the database, which can be slower than just making changes directly. The second level cache is fantastic for read-only data, however.

Tuning Hibernate and the caches can be quite challenging and complex. I highly recommend the use of a tool such as p6spy to see the database traffic between your application and the database.


You can use the Hibernate Second level Cache. Here's a good tutorial,

http://www.javalobby.org/java/forums/t48846.html


<?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>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/empi</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">admin</property>
<!--         <property name="show_sql">true</property>  -->
    <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <mapping resource="com/mycomp/myproj/pkg/Patient.hbm.xml"/>
        <mapping resource="com/mycomp/myproj/pkg/Xref.hbm.xml"/>
        <mapping resource="com/mycomp/myproj/myprojHibernateConfig/myprojPatient.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜