0

Here is my service method:

@Resource
private PersonRepository personRepository;

@Transactional
@Override
public Person create(PersonDTO created) {
    LOGGER.debug("Creating a new person with information: " + created);

    Person person = Person.getBuilder(created.getFirstName(), created.getLastName()).build();

    personRepository.save(person);

    foo();

    return null;
}
public void foo() {
    throw new RuntimeException();
}

As you can see i'm throwing an execption but persistancy takes place non the less.

Here is my datasource config:

<tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true" />
 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/dbb"/>
    <property name="username" value="root" />
    <property name="password" value="pass" />
    <property name="initialSize" value="10"/>
    <property name="maxActive" value="100"/>
    <property name="maxIdle" value="15"/> 
    <property name="minIdle" value="10"/> 
    <property name="timeBetweenEvictionRunsMillis" value="10000"/> 
    <property name="minEvictableIdleTimeMillis" value="60000"/> 
    <property name="validationQuery" value="/* ping */ SELECT 1"/>
    <property name="testOnBorrow" value="true"/> 
    <property name="testWhileIdle" value="true"/>           
    <property name="removeAbandoned" value="true"/> 
    <property name="removeAbandonedTimeout" value="300"/> 
</bean>

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="punit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>
            <!-- <property name="databasePlatform" value="${hibernate.dialect}"/> -->
            <property name="showSql" value="false"/>               
            <property name="generateDdl" value="false"/>
            <!--  <property name="hibernate.connection.autocommit" value="false"/> -->
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.connection.autocommit" value="false" />
        </map>
    </property>
</bean> 

Any idea why?

Thanks!

Urbanleg
  • 6,252
  • 16
  • 76
  • 139
  • 1
    Duplicate instance of the service (one with and one without transactions) or MySQL with **MyISAM** tables (which doesn't support transactions). Note on your configuration `hibernate.connection` properties aren't doing anything (but cluttering your XML) as you are injecting a `DataSource`. – M. Deinum Nov 25 '13 at 10:48
  • We're using INNODB, and why exactly do you think I'm having a duplicated service? – Urbanleg Nov 25 '13 at 10:57
  • 3
    You wouldn't be the first. If you are using component-scanning make sure that your `ContextLoaderListener` and `DispatcherServlet` don't scan for the same components. Also make sure that the service is loaded in the context where the `` is specified. – M. Deinum Nov 25 '13 at 11:03
  • M.Denium , Thanks it works, I scanned the components twice. – Urbanleg Nov 25 '13 at 11:10

1 Answers1

-3

You have to throw TransactionSystemException of Spring. Otherwise, you have to throw own exception that extend TransactionSystemException.

Example :

import org.springframework.transaction.TransactionSystemException;

public class SystemException extends TransactionSystemException {
    public SystemException(String message) {
        super(message);
    }

    public SystemException(String message, Throwable throwable) {
        super(message, throwable);
    }
}

public void foo() {
    throw new SystemException("System Error!");
}   
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • No you don't... Springs transaction management follows the same semantics as the EJB specs and does a rollback on all the runtime exceptions by default. See [section 12.5](http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/htmlsingle/#transaction-declarative) of the reference guide. – M. Deinum Nov 25 '13 at 10:53
  • Spring rolls back for each RuntimeException by default – Adam Dyga Nov 25 '13 at 10:57