Results 1 to 5 of 5

Thread: Need Help: Managing Hibernate Session at EJB layer

  1. #1
    Join Date
    Oct 2005
    Posts
    80

    Unhappy Need Help: Managing Hibernate Session at EJB layer

    This is my architecture:
    @Stateless EJB 3 Services --> Business Services (Spring) --> DAO via HibernateDaoSupport (Spring)

    I am using CMT in an application server. I need the Hibernate session available to me in EJB layer for lazy loading/object conversion; unfortunately it only opens/closes at my Business Service layer because that's the boundaries of Spring's transactional support.

    How can I push Spring's transactional support to the EJB 3 layer? I am looking to automatically open and close the Hibernate session there? This has been a problem plaguing me for months; haven't found any solution.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Configure everything correctly... YOu need a JTA transaction manager and you need a proper setup of hibernate to run in a JTA environment. Next to that also make sure your EJB is managing your transaction and not spring.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2005
    Posts
    80

    Default

    This is my plan, as of now:
    1) XA database driver
    2) Use <tx:jta-transaction-manager> in Spring
    3) Inject a Spring TransactionTemplate into my EJB to wrap the delegation in a transaction; this will make one Hibernate Session available to my business objects ... but most importantly, I will be able to access the lazy-loaded properties that return from the service method

    Example:
    Code:
    @Stateless
    @Interceptors(SpringBeanAutowiringInterceptor.class)
    public class SampleEJB {
    
        @Autowired
        private SpringManagedService service;
        @Autowired
        private TransactionTemplate t;
    
        public void foo() {
            t.execute(new TransactionCallbackWithoutResult() {
                @Override
                doInTransactionWithoutResult(TransactionStatus status) {
                    MyEntity entity = service.foo();
                    entity.lazyCollection().iterator(); // access lazy properties; hibernate session still open
                }
            });
        }
    }
    Two questions though:
    A) Is #3 the right strategy? It's my version of OpenSessionInViewFilter for EJB
    B) Should I set "hibernate.transaction.factory_class" and "hibernate.transaction.manager_lookup_class" in my SessionFactory properties? They are not set.

    Thanks
    Last edited by paul4christ79; Feb 15th, 2013 at 09:05 AM.

  4. #4
    Join Date
    Oct 2005
    Posts
    80

    Default

    BTW, for anyone browsing this thread, the topic has been discussed before with 2 possible solutions:
    http://forum.springsource.org/showth...pation-problem

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    A) Is #3 the right strategy? It's my version of OpenSessionInViewFilter for EJB
    No... Configure hibernate/jta correctly..

    B) Should I set "hibernate.transaction.factory_class" and "hibernate.transaction.manager_lookup_class" in my SessionFactory properties? They are not set.
    Yes (or no depending on your hibernate version if you use Hibernate 4+ configure the JtaPlatform instead).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •