Announcement Announcement Module
Collapse
No announcement yet.
Exception handling not working in Spring + JSF Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception handling not working in Spring + JSF

    Hello, I'm using JSF + Spring + JPA and a DB2 database to build an application. For a many-to-many relationship I created a link table that links two other tables. This has to be done manually because the user has to select records from both linked tables and create an entry in the link table which contains both foreign keys (the usual way).

    The point is, this may try to insert a record that is already in the linked table, in which case the DBM will issue an error that will be reflected as a JPA exception (this is OK).

    I'm trying to handle the exception at the JSF-Spring controller level (or managed bean, if you wish). To do this, I just propagate the exception in the DAO by means of this code:

    Code:
    public <T> void grabaTabla(T entityBean) throws DataAccessException,
                JpaSystemException {
            em.persist(entityBean);
            return;
        }
    which corresponds to the code that persists the Entity in DB2. To catch and process the exception, I've coded the following in the controller:

    Code:
      .
                .
                try {
                    ndbDao.grabaTabla(profAc);
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.print("Me cai por culpa de la excepción:" + e);
                    e.printStackTrace();
                }
    I used the Exception object after using both DataAccessException (that is the generic exception class for Spring) and JPASystemException (which is the specific exception thrown by OpenJPA after DB2 error); however none of them is intercepted and as a result, I get an ugly stack trace shown instead of a JSF message telling the user he's trying to insert a record that's already there.

    I´m including the Spring context file:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
        default-autowire="byName">
        <bean
            class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
        <context:annotation-config />
        <context:load-time-weaver />
        <bean class="cl.nanbei.java.dao.NanbeiDBImpl" />
        <bean id="emf"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="NanbeiJPA_V3" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
                    <property name="database" value="DB2" />
                    <property name="showSql" value="true" />
                    <property name="generateDdl" value="false" />
                </bean>
            </property>
            <property name="dataSource" ref="dataSource" />
        </bean>
        <!--
            <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
            <property name="url" value="jdbc:db2://LinuxHP:50001/NANBEI" />
            <property name="username" value="db2inst1" /> <property
            name="password" value="cba11gaj" /> </bean>
        -->
        <jee:jndi-lookup jndi-name="java:comp/env/jdbc/NanbeiDB"
            id="dataSource" />
    
        <tx:annotation-driven />
        <bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <description>Administrador de Transacciones para JPA
            </description>
            <property name="dataSource" ref="dataSource" />
            <property name="entityManagerFactory" ref="emf" />
        </bean>
    </beans>
    and parts of the Tomcat console log:
    Code:
    SEVERE: org.springframework.orm.jpa.JpaSystemException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.; nested exception is <openjpa-1.2.1-r752877:753278 fatal general error> org.apache.openjpa.persistence.PersistenceException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.
    Throwable occurred: javax.faces.el.EvaluationException: org.springframework.orm.jpa.JpaSystemException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.; nested exception is <openjpa-1.2.1-r752877:753278 fatal general error> org.apache.openjpa.persistence.PersistenceException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.
                                            .
                                            .
    Caused by: org.springframework.orm.jpa.JpaSystemException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.; nested exception is <openjpa-1.2.1-r752877:753278 fatal general error> org.apache.openjpa.persistence.PersistenceException: The transaction has been rolled back.  See the nested exceptions for details on the errors that occurred.
                                             .
                                             .
    Caused by: <openjpa-1.2.1-r752877:753278 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: DB2 SQL error: SQLCODE: -803, SQLSTATE: 23505, SQLERRMC: 1;AMERECON.PROFESOR_AREACON
    FailedObject: prepstmnt 714746522 INSERT INTO PROFESOR_AREACON (AREACON_ID, PROFESOR_ID) VALUES (?, ?) [org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement]
    The exception management mechanism seems not to work.

    What am I doing wrong? Is there something missing? Thanks for your help.
Working...
X