No Hibernate Session boundNo Hibernate Session bound to thread, and configuration does not allow cr

MultiThreading with Spring and Hibernate(MySQL)

解决方案:
1. 增加注解管理;
@transaction(promoted ,readonly )
这样子既能产生session,不报这个错误又不产生事务;
多线程   multi thread 中遇到,以为是session不安全,多线程问题..最终两次都是发现自己的问题,没有在该加 transaction 注解的地方加 transaction 注解.


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

share | edit | flag
 
 
Is that supposed to be b.setId()? –   Lee Meador  Aug 13 '13 at 17:24
 
Yep, just corrected it. Typo happened while trying to create a minimal example of my actual code. –  Rahul  Aug 13 '13 at 17:27
 
add comment
 

1 Answer

up vote 4 down vote accepted

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).

share | edit | flag
 
1
 
Thanks Jason, I din't had rollback() due to which a previous exception was leaving a transaction open for the thread and the subsequent beginTransation's used to throw "TransactionException". –   Rahul  Aug 13 '13 at 18:01
 
add comment



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值