Results 1 to 7 of 7

Thread: exception problems

  1. #1
    Join Date
    Oct 2004
    Posts
    6

    Default exception problems

    hmm at some point something changed in my application and i dont understand what.

    i save a user

    public void saveUser(User user) throws DuplicateUserException {
    try {
    getHibernateTemplate().save(user);
    } catch (DataIntegrityViolationException e) {
    // logger.error(e);
    throw new DuplicateUserException(user.getUsername());
    }
    }

    but i never get the DuplicateUserException in the bean that calls it when the users exists in the database.

    this is my stack trace

    Code:
    Hibernate: insert into USER (password, firstName, lastName, address, areaCode, email, activated, user_id) values (?, ?, ?, ?, ?, ?, ?, ?)
    06:37:25,320  WARN JDBCExceptionReporter:38 - SQL Error: 1062, SQLState: 23000
    06:37:25,323 ERROR JDBCExceptionReporter:46 - Duplicate key or integrity constraint violation message from server: "Duplicate entry '1' for key 1"
    06:37:25,331  WARN JDBCExceptionReporter:38 - SQL Error: 1062, SQLState: 23000
    06:37:25,333 ERROR JDBCExceptionReporter:46 - Duplicate key or integrity constraint violation message from server: "Duplicate entry '1' for key 1"
    06:37:25,340 ERROR JDBCExceptionReporter:38 - Could not execute JDBC batch update
    java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '1' for key 1"
    ......
    ......
    
    06:37:25,350 ERROR SessionImpl:2379 - Could not synchronize database state with session
    net.sf.hibernate.JDBCException: Could not execute JDBC batch update
    ......
    ......
    Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '1' for key 1"
    .....
    .....
    06:37:25,370 ERROR UserBean:70 - Could not add user
    org.springframework.dao.DataIntegrityViolationException: Hibernate operation: Duplicate key or integrity constraint violation message from server: "Duplicate entry '1' for key 1"; nested exception is java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '1' for key 1"
    java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '1' for key 1"

    in my applicationcontext.xml i have

    Code:
      <bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
        </bean>
    
      <bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
            <property name="sessionFactory">
                <ref bean="sessionFactory"/>
            </property>
            <property name="jdbcExceptionTranslator">
                <ref bean="jdbcExceptionTranslator"/>
            </property>
        </bean>
    
    and i also have a hibernateinterceptor to be able to use lazy loading
    
      <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
            <property name="sessionFactory">
                <ref bean="sessionFactory"/>
            </property>
        </bean>

    what could have happend?

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

    Default

    Puzzling. I assume that commented out log line doesn't fire? Can you try debugging to look at the exception? Just in case you're getting a different subclass of DataAccessException (which looks unlikely) you could try broadening the catch clause to DataAccessException : not as a recommended code change, of course, but as a diagnostic step.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Oct 2004
    Posts
    6

    Default

    i don't even get the duplicateuserexception if i catch Exception

    Code:
       public void saveUser&#40;User user&#41; throws DuplicateUserException &#123;
            try &#123;
                getHibernateTemplate&#40;&#41;.save&#40;user&#41;;
            &#125; catch &#40;Exception e&#41; &#123;
                System.out.println&#40;"---------------------------------------"&#41;;
                this.logger.error&#40;"In Exception"+e&#41;;
                throw new DuplicateUserException&#40;user.getUsername&#40;&#41;&#41;;
            &#125;
        &#125;

    Here is the full config..

    Code:
    <beans>
       <!-- DataSource Definition -->
        <bean id="dataSource"
            class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName">
                <value>com.mysql.jdbc.Driver</value>
            </property>
            <property name="url">
                <value>jdbc&#58;mysql&#58;//localhost/database</value>
            </property>
            <property name="username">
                <value>username</value>
            </property>
            <property name="password">
                <value>secret</value>
            </property>
        </bean>
    
        <!-- Hibernate SessionFactory Definition -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="mappingResources">
                <list>
                    <value>dk/bytenbog/model/businessobject/User.hbm.xml</value>
                </list>
            </property>
    
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                    <prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.HashtableCacheProvider</prop>
                </props>
            </property>
    
            <property name="dataSource">
                <ref bean="dataSource"/>
            </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>
    
        <!-- Hibernate Template Defintion -->
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
            <property name="sessionFactory">
                <ref bean="sessionFactory"/>
            </property>
            <property name="jdbcExceptionTranslator">
                <ref bean="jdbcExceptionTranslator"/>
            </property>
        </bean>
    
        <!-- User DAO object&#58; Hibernate implementation -->
        <bean id="userDao" class="dk.bytenbog.model.dao.hibernate.UserDaoHibernateImpl">
            <property name="hibernateTemplate">
                <ref bean="hibernateTemplate"/>
            </property>
        </bean>
    
        <!-- Hibernate Transaction Manager Definition -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="sessionFactory"/>
            </property>
        </bean>
    
        <!--  User Service Defintion -->
        <bean id="userServiceTarget" class="dk.bytenbog.model.service.impl.UserServiceImpl">
            <property name="userDao">
                <ref local="userDao"/>
            </property>
        </bean>
    
    
    
        <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target">
                <ref bean="userServiceTarget"/>
            </property>
            <property name="proxyInterfaces">
                <value>dk.bytenbog.model.service.UserService</value>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>hibernateInterceptor</value>
                </list>
            </property>
        </bean>
    
    
    
        <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
            <property name="sessionFactory">
                <ref bean="sessionFactory"/>
            </property>
        </bean>
    
    </beans>

  4. #4
    Join Date
    Oct 2004
    Posts
    6

    Default

    by removing the
    proxy i can get the exception.

    any suggestion to what might be the problem?

  5. #5
    Join Date
    Oct 2004
    Posts
    6

    Default

    i also get exceptions when i try and update a object

    net.sf.hibernate.HibernateException: Batch update row count wrong: 0

    and its only when lazy loading is turned on with the proxy and hibernate interceptor...
    what am i doing wrong with my config ?


    to me it looks like im doing excatly like described here
    http://www.jroller.com/page/kbaum/20...ation_with_dao
    Being Lazy in the Business Layer

  6. #6
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Keep in mind that if you are in a wrapping transaction, your actual save call is not generally going to trigger any database write (although it can, depending on the strategy for generating an id, since the id could be generated via the PK being an identity column in something like SQL Server or MySQL). So you will only get the actual exception when, higher up, your transaction commits and Hibernate does a flush. Are you 100% sure that the error trace you have in the log is actually from the point where you are trying to catch the error, and not from the transaction commit?

    Try adding a flush (HibernateTemplate has a flush method) immediately after your save, and see if it makes a difference. Note also that running a query also makes Hibernate generally do a flush.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  7. #7
    Join Date
    Oct 2004
    Posts
    6

    Default

    Thanks to the both of you.

    flush did the trick for the exception problem

    and "Batch update row count wrong: 0" was because i was using saveOrUpdate on a object with assigned identity

Similar Threads

  1. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  2. MVC step-by-step problems
    By joegaber in forum Web
    Replies: 2
    Last Post: Jul 25th, 2005, 10:16 AM
  3. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  4. Replies: 0
    Last Post: Jul 11th, 2005, 05:49 PM
  5. Replies: 3
    Last Post: Nov 8th, 2004, 07:30 PM

Posting Permissions

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