Hibernate 多线程问题!

09-06-10 pliaozrlp
程序如下:

//主调用程序

public void testGetPerson()throws Exception{

Thread A1 = new Thread(new A());

A1.start();

//A1.sleep(10000);

Thread A2 = new Thread(new A());

A2.start();

//A2.sleep(10000);

Thread A3 = new Thread(new A());

A3.start();

//A3.sleep(10000);

}

//线程定义

class A implements Runnable{

public void run() {

System.err.println(Thread.currentThread().getId()+"thread begin...");

Session se11 = HibernateSessionFactory.getSession();

System.err.println(Thread.currentThread().getId()+"opensession");

Transaction tx = se11.beginTransaction();

System.err.println(Thread.currentThread().getId()+"beginTran");

Person person = new Person();

person.setName(Thread.currentThread().getId()+"testName");

person.setSex("boy");

se11.save(person);

System.err.println(Thread.currentThread().getId()+"save");

tx.commit();

System.err.println(Thread.currentThread().getId()+"commit");

se11.close();

System.err.println(Thread.currentThread().getId()+"sessionclose");

}

}

这个程序中控制台的输出是:

9thread begin...

8thread begin...

10thread begin...

这样就退出了。不知道是什么问题,如果把线程sleep的方法去掉注释就可以完成!大家帮忙看看,在此谢谢了!

HibernateSessionFactory是myeclipse自动生成的,它的主要方法是:

static{

try {

configuration.configure(configFile);

sessionFactory = configuration.buildSessionFactory();

} catch (Exception e) {

System.err

.println("%%%% Error Creating SessionFactory %%%%");

e.printStackTrace();

}

}

private HibernateSessionFactory() {

}

/**

* Returns the ThreadLocal Session instance. Lazy initialize

* the <code>SessionFactory</code> if needed.

*

* @return Session

* @throws HibernateException

*/

public static Session getSession() throws HibernateException {

Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {

if (sessionFactory == null) {

rebuildSessionFactory();

}

session = (sessionFactory != null) ? sessionFactory.openSession()

: null;

threadLocal.set(session);

}

return session;

}

[该贴被pliaozrlp于2009-06-10 11:36修改过]

banq
2009-06-12 08:57
断点跟踪跟踪一下吧

pliaozrlp
2009-06-18 15:45
谢谢banq!已经解决了,这个是因为junit不支持多线程单元调试导致的!如果写在main中就没问题了!

猜你喜欢