2012-03-19 65 views
0

我有 addAgent定義爲Spring事務鏈接

@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); 
} 

addUser用戶方法本身是事務性的服務的方法。 每當我試圖保存數據。它成功執行前兩個,但失敗的年齡創建。

代碼與junit一起工作正常。

它似乎與事務設置問題的問題。意味着交易中的交易。

任何機構可以幫助我如何做到這一點的交易在春季3

在登錄

鏈的似乎是這樣

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 

可能

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> 

找到另一個日誌:

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 
+1

它是如何失敗?什麼是錯誤? – 2012-03-19 11:51:47

+0

問題沒有錯誤日誌在服務器(春季調試是),但最後一行(代理=(代理)genericDAO.create(代理);)執行成功,但代理ID是自動生成仍然爲空。 – 2012-03-19 11:59:19

+0

看到這個:http://www.ibm.com/developerworks/java/library/j-ts1/index.html#listing4 – subodh 2012-03-19 12:02:42

回答

0

的問題是固定的發射。但是,線束是我不滿意的解決方案。

我正在使用Spring @Autowire註解注入對象。我刪除了並手動注入。我工作得很好。

我有點確定註冊事務使用註釋 (可能是事務代理沒有包裝服務)需要一些設置。

夥計們,如果任何一個想法我們錯過了什麼設置我附上我的applicationContext.xml上面。

0

請檢查this

發表你的bean definations,如果你仍然面臨的一個問題。

編輯

添加以下行到你的日誌屬性

log4j.logger.org.hibernate.type=trace 

,並檢查什麼都插入越來越有價值觀

+0

請查找附加的bean定義。 – 2012-03-19 13:14:14

+0

使用服務層將數據保存到數據庫中。但單元測試沒有這個問題。我打開調試並仔細檢查日誌文件。在數據對數據庫惰性化的地方,我發現信息:「由於沒有正在進行的交易而延遲身份插入」。 ..很好地幫助我。 – 2012-03-19 13:21:09

+0

insert for this.userManagerService.addUser(userProfile.getEmail(),password,new String [] {agent.getAgentType()},userProfile,false); this.userProvisioningService.enableUser(userProfile.getEmail());工作正常,因爲正在使用mybatis插入到數據庫(我可以看到這些插入日誌。 – 2012-03-20 04:23:06