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:
My applicationContext File...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; }
Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java: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: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 (JTA) --> <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>


Reply With Quote
