首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring事务链接

Spring事务链接
EN

Stack Overflow用户
提问于 2012-03-19 19:48:39
回答 2查看 2.3K关注 0票数 0

我有一个服务方法addAgent,定义为

代码语言:javascript
运行
复制
@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上运行得很好。

它似乎与事务设置问题有关。表示事务内部的事务。

在spring 3中,有没有人能帮我解决这个事务链接的问题?

在日志中,它似乎是这样的

代码语言:javascript
运行
复制
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

可能是

代码语言:javascript
运行
复制
delaying identity-insert due to no transaction in progress

applicationContext.xml

代码语言:javascript
运行
复制
<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>

查找另一个日志:

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-20 15:28:23

问题已解决。但驾驭是我对解决方案不满意。

我是使用Spring @Autowire注解注入对象的。我把它取下来,然后手动注射。我工作得很好。

我确信使用注解注入事务需要一些设置(可能是事务代理没有包装服务)。

伙计们,如果有人知道我们错过了什么设置,我在上面附上了我的applicationContext.xml。

票数 0
EN

Stack Overflow用户

发布于 2012-03-19 21:05:24

请查看this

发布你的bean定义,以防你仍然面临问题。

编辑

将以下行添加到日志属性中

代码语言:javascript
运行
复制
log4j.logger.org.hibernate.type=trace

并检查使用值触发的所有插入的内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9769594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档