Announcement Announcement Module
Collapse
No announcement yet.
Translate java.sql.SQLException to DataAccessException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Translate java.sql.SQLException to DataAccessException

    Hallo.
    Since it seems that I cannot use the spring DataAccessException translation mechanism in my dao, I would like to know if it possible to translate the
    Code:
    Internal Exception: java.sql.SQLException: [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (JSP_OWN.IDX_MC_CC_RAPPORTI_02) violated
    to the DataAccessException hierarchy manually.

    Kind regards
    Massimo

  • #2
    First, I'd like to know why doesn't the automatic translation work. Do you annotate your daos with @Repository? Because it's as simple as that...

    Nonetheless, if I had to do this by hand, I would use an after throwing advice that catches SQLExceptions and rethrows a DataAccessException with the SQLException wrapped inside (which I guess is what the automatic mechanism does). I'd then write a pointcut that matches all methods in my DAOs, and force my dao methods to throw SQLException by specifying the throws clause in the interfaces (I think the automatic mechanism uses a pointcut that matches all methods in classes annotated with @Repository).

    Comment


    • #3
      Hallo Enrico.

      First, I'd like to know why doesn't the automatic translation work. Do you annotate your daos with @Repository? Because it's as simple as that...
      As I described in other threads
      I had several problems configuring the exception translation on my daos.

      I have an event-driven enterprise application with this configuration:
      - spring, spring-integration for all the tiers
      - JPA with eclipse link as jpa provider for persistence on Oracle 10
      - org.springframework.transaction.jta.JtaTransaction Manager as transaction manager
      - AOP on service methods for Transacitions
      - MDB for events to read and process the message
      - Weblogic 10gR3 as AS

      I use the generic dao pattern for all of my domain model, so first I annotated the generic DAO with @Repository

      Code:
      @Repository
      public class GenericDaoJpaImpl<T, ID extends Serializable> implements GenericDao<T, ID> ...
      Then I enabled these post processors on my spring configuration:

      Code:
      <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
      <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
      Ok. With this configuration the only way to make the exception translation work was to couple the entityManager.flush() method on every save or update method that could throws any exception to be translated.

      Since I use AOP transaction on service method as described below

      Code:
      @Override
      	@Transactional(propagation = Propagation.REQUIRES_NEW)
      	public EventMessage<VariazioneOperativitaDerivati> activate(EventMessage<VariazioneOperativitaDerivati> eventMessage) {
      		.....
      		rapportoDao.saveItem(depositoAmministratoLoaded);
      		.....
      	}
      If the method rapportoDao.saveItem(depositoAmministratoLoaded) throws a database exception I suppose that since the commit is done by JTA at the end of the method and not immediatly after the save, the exception translation trick is not executed as described by this stacktrace:

      Code:
      2011-06-17 16:30:12,671 DEBUG 01025649900982072 - 009 - 1747 [ErrorHandlerActivator] Nested exception cause {} 
      org.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is weblogic.transaction.RollbackException: Unexpected exception in beforeCompletion: sync=[email protected]67210
      
      Internal Exception: java.sql.SQLException: [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (JSP_OWN.IDX_MC_CC_RAPPORTI_02) violated
      
      Error Code: 1
      Call: INSERT INTO S_MC_CC_RAPPORTI (COD_RAPPORTO, COD_MATRICOLAGESTORE, DESC_ATTRIBUTO, COD_TIPORAPPORTO, COD_ABI, NUM_RAPPORTO, FLAG_RENDICONTAZIONE, COD_CATEGORIA, COD_STATORAPPORTO, FLAG_SB_ATTIVO, COD_GESTORE, TIMESTAMP_AGGIORNAMENTO, COD_NSG, DEN_FILIALE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
      	bind => [1234567, null, CC1000S0, 1 , 01025, 87654321, null, 3516, null, null, null, null, null, 00521]
      Query: InsertObjectQuery([ContoCorrente@184f13b'[ContoCorrente@184f13b'codiceRapporto1234567''abi=01025''filiale=[Filiale@1d22e6b codiceFiliale= = '00521''descrizioneFiliale=TORINO - AGENZIA 21']''categoria=3516''numeroRapporto=87654321''formaTecnica=CC1000S0''tipoRapporto=null''statoRapporto=null''rendicontazione=null''smallBusinessAttivo=null']''coordinateBancarieInterne=null''coordinateBancarieVisualizzate=null''codiceServizio=null'])
      	at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1012)
      	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
      	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
      	at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
      	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      In effect there's no mention of my save method.

      So the only way to make the machine works was coupling the save method with a call to the entityManage.flush();
      Code:
      @Override
      	public void saveItem(T instance) {
      		entityManager.persist(instance);				
                      entityManager.flush()		
      	}
      I think this is awful since it flushes all the entities of my persistence context, so I discard the solution.

      This is the summary of the jungle where I live at this moment

      Now I don't know how to proceed to get the machine work.

      Do you think I'm making some mistakes?

      Kind regards
      Massimo

      Comment

      Working...
      X