日期:2014-05-19 浏览次数:20881 次
package hibernatetest;
import packages
public class CacheTest {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.addClass(User.class);
SessionFactory sf = conf.configure().buildSessionFactory();
Thread t1 = new Thread(new UserThread(sf));
Thread t2 = new Thread(new UserThread(sf));
t1.start();
t2.start();
t1.join();
t2.join();
sf.close();
}
}
class UserThread implements Runnable {
SessionFactory sf;
public UserThread(SessionFactory sf) {
this.sf = sf;
}
public void run() {
Session session = sf.openSession();
System.out.println(Thread.currentThread().getName() + " "
+ session.hashCode());
Transaction tx = session.beginTransaction();
User user = (User) session.load(User.class, "1");
System.out.println(Thread.currentThread().getName() + " "
+ user.hashCode());
tx.commit();
session.close();
}
}
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">
oracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:master
</property>
<property name="connection.username">xxxxx</property>
<property name="connection.password">xxxxx</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
</session-factory>
</hibernate-configuration>