No Hibernate Session boundNo Hibernate Session bound to thread, and configuration does not allow cr
来源:互联网 发布:linux 安装jdk1.7 编辑:程序博客网 时间:2024/06/01 10:32
MultiThreading with Spring and Hibernate(MySQL)
How to use Hibernate in a multi threaded application?
脱离spring对hibernate session的管理,自己手动维护管理.
I am trying to use Hibernate for a multi threaded application wherein each thread retrieves an object and tries to insert it into a table. My code looks like below. I have local hibernate Session objects per thread and in each InsertData I do beginTransaction and commit.
The problem I am facing is that many times I get "org.hibernate.TransactionException: nested transactions not supported"
Since I am new to hibernate I don't know if what I am doing is correct or not? Please let me know what is the correct way to use hibernate in multi threaded app and how to avoid the above mentioned exception.
Thanks
public class Worker extends Thread {private Session session = null;Worker() { SessionFactory sf = HibernateUtil.getSessionFactory(); // Singleton session = sf.openSession(); session.setFlushMode(FlushMode.ALWAYS);}public void run() { // Some loop which will run thousand of times for (....) { InsertData(b); } session.close();}// BlogPost Table has (pk = id AutoGenerated), dateTime, blogdescription etc. private void InsertData(BlogPost b) { session.beginTransaction(); Long id = (Long) session.save(b); b.setId(id); session.getTransaction().commit();}}
My hibernate config file has c3p0.min_size=10
and c3p0.max_size=20
1 Answer
With session-objects-per-thread, as long as you are not sharing session objects between multiple threads, you will be fine.
The error you are receiving is unrelated to your multithreaded usage or your session management. Your usage of session.save()
as well as explicitly setting the ID is not quite right.
Without seeing your mapping for BlogPost
its hard to tell, but if you have told Hibernate to use the id
field as the primary key, and you are using the native generator for primary keys, the all you need to do is this:
session.beginTransaction();session.persist(b);session.flush(); // only needed if flush mode is "manual"session.getTransaction().commit();
Hibernate will fill in the ID for you, persist()
will cause the insert to happen within the bounds of the transaction (save()
does not care about transactions). If your flush mode is not set to manual then you don't need to call flush()
as Transaction.commit()
will handle that for you.
Note that with persist()
, the BlogPost's ID is not guaranteed to be set until the session is flushed, which is fine for your usage here.
To handle errors gracefully:
try { session.beginTransaction(); try { session.persist(b); session.flush(); // only needed if flush mode is "manual" session.getTransaction().commit(); } catch (Exception x) { session.getTransaction().rollback(); // log the error }} catch (Exception x) { // log the error}
By the way, I suggesting making BlogPost.setId()
private, or package visible. It is most likely an implementation error if another class sets the ID explicitly (again assuming native generator, and id as primary key).
- No Hibernate Session boundNo Hibernate Session bound to thread, and configuration does not allow cr
- Spring+Hibernate 异常No Hibernate Session bound to thread, and configuration does not allow creation
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactiona
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactiona
- @Transactional未添加就报错:No Hibernate Session bound to thread, and configuration does not allow creation
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional
- No Hibernate Session bound to thread, and configuration does not allow 解决办法
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactiona
- No Hibernate Session bound to thread, and configuration does not allow create解决方案
- No Hibernate Session bound to thread, and configuration does not allow creat
- No Hibernate Session bound to thread, and configuration does not allow create
- HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional
- 解决No Hibernate Session bound to thread, and configuration does not allow create tansactional的问题
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional
- No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional
- No Hibernate Session bound to thread, and configuration does not allow creation
- Android开发学习笔记:EditText的属性介绍
- Android开发学习笔记:TextView的属性详解
- C++[类设计]使用数组实现顺序栈
- 程序员内功大法
- String创建对象问题
- No Hibernate Session boundNo Hibernate Session bound to thread, and configuration does not allow cr
- ubuntu zend studio 5.5 解决中文乱码问题
- ZOJ-1857
- 第7周作业1-循环大战------while
- windows驱动在线帮助
- Python3.2 官方文档翻译--错误与异常
- Python3.2官方文档翻译--异常处理
- C++primer plus第六版课后编程题答案12.2
- jQuery ajax - getScript() 方法
b.setId()
? – Lee Meador Aug 13 '13 at 17:24