0

i have a service method addAgent defined as

@Override
@Transactional(readOnly=false)
public void addAgent(Agent agent, String password,UserProfile userProfile) {
    this.userManagerService.addUser(userProfile.getEmail(), password, new    String[]  {agent.getAgentType()}, userProfile,false);
    this.userProvisioningService.enableUser(userProfile.getEmail());
    agent.setUserProfileId(userManagerService.getUserProfileId(userProfile.getEmail()));
    agent = (Agent)genericDAO.create(agent);
}

The addUser user method is itself is transactional. Whenever i am trying to save the data. It execute successfully for first two but fails for age create.

The code is working fine with junit.

It seems issue with transaction setting issue. Means transaction inside transaction.

Can any body help me out how to do this transaction chaining in spring 3.

in logs its seem to be like this

2012-03-19 17:20:28,945 [TP-Processor2] DEBUG jpa.JpaTemplate - Creating new EntityManager for JpaTemplate execution
2012-03-19 17:20:28,949 [TP-Processor2] DEBUG impl.SessionImpl - opened session at timestamp: 13321578289
2012-03-19 17:20:29,161 [TP-Processor2] DEBUG def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
2012-03-19 17:20:29,163 [TP-Processor2] DEBUG jpa.JpaTemplate - Closing new EntityManager after JPA template execution
2012-03-19 17:20:29,164 [TP-Processor2] DEBUG jpa.EntityManagerFactoryUtils - Closing JPA EntityManager

might be

delaying identity-insert due to no transaction in progress

applicationContext.xml

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="txManager" />
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="B2C_BROKER" />
        <property name="jpaVendorAdapter">
            <bean id="jpaAdapter"
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="false" />
                <property name="generateDdl" value="false" />
            </bean>
        </property>
    </bean> 
    <bean id="genericDAO" class="com.core.orm.dao.GenericDAOImpl">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

find another log:

2012-03-19 20:13:21,074 [TP-Processor3] DEBUG jpa.JpaTemplate - Creating new EntityManager for JpaTemplate execution
2012-03-19 20:13:21,082 [TP-Processor3] DEBUG impl.SessionImpl - opened session at timestamp: 13321682010
2012-03-19 20:13:21,082 [TP-Processor3] DEBUG impl.SessionImpl - opened session at timestamp: 13321682010
2012-03-19 20:13:21,090 [TP-Processor3] TRACE impl.SessionImpl - setting flush mode to: AUTO
2012-03-19 20:13:21,090 [TP-Processor3] TRACE impl.SessionImpl - setting flush mode to: AUTO
2012-03-19 20:13:21,096 [TP-Processor3] TRACE impl.SessionImpl - setting cache mode to: NORMAL
2012-03-19 20:13:21,096 [TP-Processor3] TRACE impl.SessionImpl - setting cache mode to: NORMAL
2012-03-19 20:13:21,100 [TP-Processor3] TRACE def.AbstractSaveEventListener - transient instance of: com.xchange.agent.domain.Agent
2012-03-19 20:13:21,100 [TP-Processor3] TRACE def.AbstractSaveEventListener - transient instance of: com.xchange.agent.domain.Agent
2012-03-19 20:13:21,103 [TP-Processor3] TRACE def.DefaultPersistEventListener - saving transient instance
2012-03-19 20:13:21,103 [TP-Processor3] TRACE def.DefaultPersistEventListener - saving transient instance
2012-03-19 20:13:21,106 [TP-Processor3] TRACE def.AbstractSaveEventListener - saving [com.xchange.agent.domain.Agent#<null>]
2012-03-19 20:13:21,106 [TP-Processor3] TRACE def.AbstractSaveEventListener - saving [com.xchange.agent.domain.Agent#<null>]
2012-03-19 20:13:21,110 [TP-Processor3] DEBUG def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
2012-03-19 20:13:21,110 [TP-Processor3] DEBUG def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress
2012-03-19 20:13:21,114 [TP-Processor3] DEBUG jpa.JpaTemplate - Closing new EntityManager after JPA template execution
2012-03-19 20:13:21,115 [TP-Processor3] DEBUG jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
2012-03-19 20:13:21,117 [TP-Processor3] TRACE impl.SessionImpl - closing session
2012-03-19 20:13:21,117 [TP-Processor3] TRACE impl.SessionImpl - closing session
2012-03-19 20:13:21,119 [TP-Processor3] TRACE jdbc.ConnectionManager - connection already null in cleanup : no action
2012-03-19 20:13:21,119 [TP-Processor3] TRACE jdbc.ConnectionManager - connection already null in cleanup : no action
Zuned Ahmed
  • 1,357
  • 6
  • 29
  • 56
  • 1
    how does it fail? what is the error? – andrew cooke Mar 19 '12 at 11:51
  • problem there is no error logs in server (spring debug is on) but the last line ( agent = (Agent)genericDAO.create(agent);) executes successfully but the agent id is which is auto generated is still null. – Zuned Ahmed Mar 19 '12 at 11:59
  • see this: http://www.ibm.com/developerworks/java/library/j-ts1/index.html#listing4 – subodh Mar 19 '12 at 12:02
  • i have attached logs @crooke. – Zuned Ahmed Mar 19 '12 at 12:08
  • i am not sure i understand, but i don't see a transaction starting in the logs. the class instance that contains the addAgent method is declared as a bean in your spring config, right? and the call to addAgent is coming from another class (rather from inside the same class), right? because either of those can cause the transaction to be missed because of poor aop, if i remember right. also, you have enabled transactions in the config as described in the links elsewhere, right? – andrew cooke Mar 20 '12 at 00:56

2 Answers2

0

Please check this

Post your bean definations, in case you still face a problem.

EDIT

Add following lines to your log properties

log4j.logger.org.hibernate.type=trace

and check what all inserts are getting fired with values

Community
  • 1
  • 1
Dhananjay
  • 3,903
  • 2
  • 29
  • 44
  • please find attached bean definition. – Zuned Ahmed Mar 19 '12 at 13:14
  • use Service layer to persist data into database. But the Unit Test does not have this problem. I turn on debug and check the log file carefully. In the place where data should inert into database, I find the information: "delaying identity-insert due to no transaction in progress". ..kindly help me out. – Zuned Ahmed Mar 19 '12 at 13:21
  • insert for this.userManagerService.addUser(userProfile.getEmail(), password, new String[] {agent.getAgentType()}, userProfile,false); this.userProvisioningService.enableUser(userProfile.getEmail()); is working fine as the are using mybatis to insert into db(i can see insert logs for these. – Zuned Ahmed Mar 20 '12 at 04:23
0

The issue is fixed. But harness is i am not satisfied with solution.

I was injecting object using Spring @Autowire annotation. I removed that and injected manually. I worked fine.

I am bit sure the there is some setting required to inject transaction using annotation (may be the transaction proxy are not wrapping around the service).

Guys if any one idea what setting we missed i am attached my applicationContext.xml above.

Zuned Ahmed
  • 1,357
  • 6
  • 29
  • 56