Results 1 to 3 of 3

Thread: Exceptions not Thrown with HibernateTransactionManager

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    Default Exceptions not Thrown with HibernateTransactionManager

    Hi All,

    For a week or so now I have been trying to amend one or our EJB calls to use spring deeclarative transaction management with hibernate. The idea being to use it in all our EJB's and then hopefully faze out our EJB's!

    1 . However I am nearly there but I need to catch duplicate key exceptions and throw my own but I think I have read too many posts and got confused since DataIntegrityViolationException is not being thrown.

    2. My config is below but first I would like to say that I have used a hibernate interrceptor in my dao instead of the template as I neeed to use the Hibernate query interface and have used the transactionInterceptor in my EJB impl. Not sure if this is the corrrect way but I I dont know if I am able to combine these somehow to put both in the dao?

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName">
    <value>java:comp/env/jdbc/osonetracker-ds</value>
    </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    <property name="mappingResources">
    <list>
    mapping files .....
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.M ySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>

    <!-- Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.Hibernate Interceptor">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor .TransactionInterceptor">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributeSource">
    <value>com.jspangle.onetracker.service.registratio n.RegistrationService.register=PROPAGATION_REQUIRE D</value>
    </property>
    </bean>

    <bean id="organisationDataAccessorImpl" class="com.jspangle.onetracker.data.access.hiberna te.OrganisationDataAccessor">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="organisationDataAccessor" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces">
    <value>com.jspangle.onetracker.data.access.IOrgani sationDataAccessor</value>
    </property>
    <property name="interceptorNames">
    <list>
    <value>hibernateInterceptor</value>
    <value>organisationDataAccessorImpl</value>
    </list>
    </property>
    </bean>

    <bean id="registrationServiceImpl" class="com.jspangle.onetracker.service.registratio n.RegistrationServiceImpl">
    <property name="organisationDataAccessor">
    <ref bean="organisationDataAccessor"/>
    </property>
    </bean>

    <bean id="RegistrationService" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces">
    <value>com.jspangle.onetracker.service.registratio n.RegistrationService</value>
    </property>
    <property name="interceptorNames">
    <list>
    <value>transactionInterceptor</value>
    <value>registrationServiceImpl</value>
    </list>
    </property>
    </bean>

    </beans>

    Code:

    public Long register(Organisation organisation){
    Long id = null;
    try {
    id = (Long)organisationDataAccessor.addOrganisation(org anisation);
    } catch (DataIntegrityViolationException e) {
    e.printStackTrace();
    }
    return id;

    Sorry to ramble but my questions are:

    1. How do I get to trap the exceptions, in my log I am getting JDBCExceptionReporter entries is this correct or should I used SQLExceptionTranslator ? if so how do I without using the HibernateeTemplate.

    2. Is my approach ok whereby the hibernate interceptor is applied to the dao and the transacfionInterceptor applied to my EJB impl? Can I merge these?

    My head is swimming .. Any help is appreciated.

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Some thoughts:

    - Use of HibernateTemplate is normally recommended. It's even possible to use Criteria queries. What is it that you can't do using HibernateTemplate?
    - You can do manual exception translation using Spring's functionality with
    Code:
    public static DataAccessException convertHibernateAccessException&#40;HibernateException&#41;;
    on org.springframework.orm.hibernate.SessionFactoryUt ils
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Feb 2005
    Posts
    4

    Default

    Hi,

    Thanks for the offer of help.

    My question are

    1. My problem is that with the supplied configuration when i insert a entity with a duplicate key a DataIntegrityViolationException is not thrown. Do I need to use JDBCExceptionReporter.


    2. I am using a hibernate interceptor instead of the HibernateTemplate since I thought it did not support Criteria queries but also that I need to make several calls on the same session and I again though the template opended and closed this for me. I will try and revert back to the HibernateTemplate. However if I do need to use the hibernateInterceptor how do I combine this with the transactionInterceptor? In the config below I wrapped the dao with the hibernate interceptor and the EJB impl with the transaction interceptor since I new to AOP and was unsure how to combine the interceptors.

    Kind Regards
    Lea.

Similar Threads

  1. Exceptions with Spring + Axis
    By derek in forum JMS
    Replies: 5
    Last Post: Oct 17th, 2005, 09:58 PM
  2. Replies: 1
    Last Post: Sep 19th, 2005, 11:55 PM
  3. Replies: 1
    Last Post: Sep 6th, 2005, 01:42 AM
  4. Replies: 2
    Last Post: Mar 29th, 2005, 07:45 AM
  5. Handling exceptions thrown during initialization
    By tgullotta in forum Container
    Replies: 1
    Last Post: Mar 7th, 2005, 11:42 AM

Posting Permissions

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