Announcement Announcement Module
Collapse
No announcement yet.
Need Help: Managing Hibernate Session at EJB layer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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, 09:05 AM.

      Comment


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

        Comment


        • #5
          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).

          Comment

          Working...
          X