1

I have spring project, with this config:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="oracle.jdbc.driver.OracleDriver" p:url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
    p:username="dev" p:password="****" >
</bean>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

    <property name="dataSource" ref="dataSource" />
     ...
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<context:component-scan base-package="ge.class.entity">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>


<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

I choose JPA transaction manager. in Java code I use EntityManager in order to persist objects.

in Spring doc I found then in order to enable nested transaction I should set flag of setNestedTransactionAllowed to true.

I dont understand where should I write this? I have only declared entity manager like this:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

@EnableTransactionManagement()
@Repository
public class MavenDaoImpl implements MavenDao {

    protected EntityManager entityManager;
    public EntityManager getEntityManager() {
        return entityManager;
    }

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
grep
  • 5,465
  • 12
  • 60
  • 112
  • 1
    The same documentation says that spring jpa does not support nested transations. The jdbc driver should support the nested transactions – Zeus Oct 20 '14 at 22:39
  • Ok but... I dont understand what to do now... how to solve my problem, I need nested transactions... – grep Oct 20 '14 at 22:42
  • Could you please elaborate on how you are trying to perform the transactions in your service? You may want to take a look at the following url http://stackoverflow.com/questions/13051204/spring-transaction-required-vs-requires-new-rollback-transaction there are properties of the transactional which I think you can use to scope the transactions. – Zeus Oct 20 '14 at 23:02
  • I know them. REQUIRES_NEW does not works for me because I do not have nested transaction enabled. So I'm finding ways of configuration - I know how to write annotations. please see that: http://stackoverflow.com/questions/26467006/how-not-to-roll-back-previous-methods-persist-data/26469270?noredirect=1#comment41587733_26469270 – grep Oct 20 '14 at 23:15
  • OK. I think I understand your problem better now. Unfortunately the spring does not add another transaction layer for you if you are calling the second method within the same Service layer. ONly if you are calling method of a different service layer spring can create another proxy(transaction boundary) for the newly called service method. I'd resort to manual transactions if I have to go through this case plus try catch block for each manual transactions so I can rollback. – Zeus Oct 20 '14 at 23:20
  • what do u think if I will change transaction manager? for instance JTA instead of JPAtransaction Manager? will there be many differences while writing codes in java jpa classes? what manager do u use? p.s I have tried try - catch but no good result. I have 2 case - all methods are roll backed or neither all - because there is no new transaction, caused by non nested transaction config. – grep Oct 20 '14 at 23:28
  • REQUIRES_NEW has nothing to do with nested transactions. It works fine with any transaction manager, out of the box. You should tell us what your actual problem is. – JB Nizet Oct 21 '14 at 07:14
  • really? hm... I have write my prblem here http://stackoverflow.com/questions/26467006/how-not-to-roll-back-previous-methods-persist-data/, no one could not manage to solve the problem... I have 2 method A and B. and from A i invoke B. B method have REQUIRES_NEW annotation. So when I throw exception from B, I want to roll back only B and not A. – grep Oct 21 '14 at 08:30

0 Answers0