Announcement Announcement Module
Collapse
No announcement yet.
CMP / Hibernate / Spring - sorting it all out Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CMP / Hibernate / Spring - sorting it all out

    First... I'm new to EJB and Spring...
    I have a rqmt to use EJB and I have decided to start with EJB 3.0 (JBoss) - I know that JBoss EJB 3 is still fluid :wink: . In my EJB I'm using Hibernate and Spring. I really like the HibernateTemplate class and way that Spring translates Hibernate exceptions into standard Spring runtime exceptions... It is brilliant. But to use the templating and exception translation I have to manage my own transactions.

    It also appears that the EJB is starting a transaction when I enter the bean. Because the EJB was starting a transaction when I entered the Bean I had to specify PROPAGATION_REQUIRES_NEW as my Spring Template isolation level. If I did not then the exceptions were not thrown until I exited the Bean and consequently were not translated.

    So, is there an overlap here that could have an effect on overall performance?

    I have read a few Blogs on Spring vs EJB but they assume a more detailed understanding of CMP which I donít have.

    Questions:
    1) Is it correct that using Sping implies that CMP will not be used and that I will have to manage my own Transactions?
    2) If the above is true, is there a way (in EJB 3.0) to state that CMP is not being used?
    3) Is there some overlap with EJB/Spring with respect to persistence?
    4) Am I on the right path?

    Thanks very much in advance...

    Code snippits:

    Code:
    @Stateless(transactionManagement=TransactionManagementType.BEAN)
    public class ActypeCrudBean implements ActypeCrud {
    ...
    ...
    public void update(ActypeTO actypeTO) {
       Actype actype = ActypeAssembler.getDO(actypeTO);
       ActypeService service = (ActypeService) context.getBean("actypeService");
       service.update(actype);
       return;
    }
    
    
    ActypeService.java
    ...
    ...
    public void update(final Actype actype) {
       try{
          this.txnTemplate.execute(new TransactionCallbackWithoutResult() {
             public void doInTransactionWithoutResult(TransactionStatus status) {
                actypeDao.update(actype);
             }
          });
       } catch (OptimisticLockingFailureException e) {
             throw new OptimisticLockingException();
       } catch (Exception e) {
             e.printStackTrace();
            throw new UndefinedIOError(e);
       }
    }
    
    
    ActypeDaoHibernate.java
    ...
    ...
    public void update(Actype actype) {
      this.getHibernateTemplate().update(actype);
      return;
    }
    My applicationContext File...

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
        
        <bean id="dataSource"  
              class="org.springframework.jndi.JndiObjectFactoryBean">
              <property name="jndiName">
              	<value>java&#58;MySqlTakeOffDS</value>
              </property>
        </bean>
        
        <bean id="sessionFactory" 
              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
              <property name="hibernateProperties">
              	<props>
              		<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
              		<prop key="hibernate.connection.autoReconnect">true</prop>
                	<prop key="hibernate.show_sql">true</prop>
                	<prop key="hibernate.c3p0.min_size">5</prop>
                	<prop key="hibernate.c3p0.max_size">20</prop>
                	<prop key="hibernate.c3p0.timeout">1800</prop>
                	<prop key="hibernate.c3p0.max_statements">50</prop>
                	<prop key="hibernate.c3p0.autoCommitOnClose">true</prop>
                	<prop key="hibernate.cache.use_query_cache">true</prop>
                	<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
    				<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
    				<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
                    <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</prop>
                    <prop key="jta.UserTransaction">java&#58;comp/UserTransaction</prop>
              	</props>
              </property>
              <property name="dataSource">
              	<ref bean="dataSource"/>
              </property>
              <property name="mappingResources">
              	<list>
            	<value>Actype.hbm.xml</value>
            	</list>
              </property>	
        </bean>
    
    	<!-- Spring Data Access Exception Translator Defintion -->
    	<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"> 
    		<property name="dataSource"><ref bean="dataSource"/></property> 
    	</bean> 
              
        <bean id="hibernateTemplate"
              class="org.springframework.orm.hibernate3.HibernateTemplate">
              <property name="sessionFactory"> <ref bean="sessionFactory"/></property>      
              <property name="jdbcExceptionTranslator"><ref bean="jdbcExceptionTranslator"/></property> 
        </bean>
    
        <bean id="actypeDao" 
              class="com.eflight.eloadsheet.daos.hibernate.ActypeDaoHibernateImpl">
              <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property>
      	</bean>
      	
        <!-- Transaction manager for a single Hibernate SessionFactory &#40;JTA&#41; -->
        <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="userTransactionName"> <value>UserTransaction</value> </property> 
        </bean>
    
        <!-- Transaction Template -->
        <bean id="txnTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager"> <ref local="transactionManager"/> </property> 
    	<property name="propagationBehaviorName">
    		<value>PROPAGATION_REQUIRES_NEW</value>
    	</property>
        </bean>
    
    
    	<!-- ACType Service Definition --> 
    	<bean id="actypeServiceTarget" class="com.eflight.eloadsheet.service.impl.ActypeServiceImpl">
    		<property name="actypeDao"><ref local="actypeDao"/></property>
    		<property name="transactionManager"><ref bean="transactionManager"/></property>
    		<property name="txnTemplate"><ref bean="txnTemplate"/></property>
    	</bean>
    	
    	<!-- Transactional proxy for the Customer Service -->
    	<bean id="actypeService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"><ref local="transactionManager"/></property>
    		<property name="target"><ref local="actypeServiceTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    				<prop key="save*">PROPAGATION_REQUIRES_NEW</prop>
    				<prop key="update*">PROPAGATION_REQUIRES_NEW</prop>
    				<prop key="delete*">PROPAGATION_REQUIRES_NEW</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="myTransactionAttribute" class="org.springframework.transaction.interceptor.DefaultTransactionAttribute">
    		<property name="propagationBehaviorName">
    			<value>PROPAGATION_REQUIRES_NEW</value>
    		</property>
    		<property name="isolationLevelName">
    			<value>ISOLATION_REPEATABLE_READ</value>
    		</property>
    	</bean>
    	
    	<bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
    		<property name="transactionAttribute">
    			<ref bean="myTransactionAttribute"/>
    		</property>
    	</bean>
    
    </beans>

  • #2
    I have a rqmt to use EJB
    What's the technical reason for using?

    Comment


    • #3
      I'm aware that there are alternatives to EJB. No argument. But to keep focused on my questions let's assume that not using EJB is not an option.

      Comment


      • #4
        My colleagues clued me in...

        So here are the answers to my questions:

        Questions:
        1) Is it correct that using Sping implies that CMP will not be used and that I will have to manage my own Transactions?
        No, Use CMP or manage your own transaction using Spring templating...
        It is better to use CMP...

        2) If the above is true, is there a way (in EJB 3.0) to state that CMP is not being used?
        Yes, you can manage you own transactions - but why?

        3) Is there some overlap with EJB/Spring with respect to persistence?
        Not really. Spring just wraps around the persistance layer (ie. Hibernate).

        4) Am I on the right path?
        No. The examples I provided showed my ignorance.. Hey but we all have to learn... The good news is that now I have a better understanding...

        All of this trouble boiled down to this missing statement in my DAO.

        this.getHibernateTemplate().flush();

        Comment


        • #5
          let's assume that not using EJB is not an option.
          OK, but you have to decide exactly what you're going to use it for.

          1) Is it correct that using Sping implies that CMP will not be used and that I will have to manage my own Transactions?
          No, Use CMP or manage your own transaction using Spring templating...
          CMP is concerned with persistence, not necessarily transactions, so it's not a matter of CMP or Spring Transaction Management.

          It is better to use CMP...
          What's the technical justification?

          Is it correct that using Sping implies that CMP will not be used
          No, but if you're using Hibernate you can do without CMP. There are compelling reasons to do so. In particular, you have a quicker development cycle as you can test Hibernate outside of the container.

          3) Is there some overlap with EJB/Spring with respect to persistence?
          Not really. Spring just wraps around the persistance layer (ie. Hibernate).
          There is if you count transaction management. Typically this is done with SLSBs, but you can do this with Spring - again there are compelling reasons to use Spring - it's much simpler and testable outside of the container, etc...

          Comment

          Working...
          X