为什么我坚持不会发生什么?

时间:2012-01-12 10:05:54

标签: java hibernate spring jpa persist

我希望我不会问你已经回答的问题,但我无法理解我的问题...我解释道:

我使用Spring和Hibernate,我有一个Manager界面,以及一个实现我的界面的Manager。经理是这样的:

@Service
@Transactional
public class ManagerImpl implements Manager {
    @PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName="Service")
    private EntityManager em;

    public void storeHistoricMessage(...params...) throws DBException{
        HistoricMessage historicMessage = new HistoricMessage(); 
        // ... initialization of historicMessage
        try {
            em.persist(historicMessage);
        }
        catch (Exception e){
            // process the exception if exists... 
        }
    }
}

我这样称呼我的服务:

@Service
public class OtherManager {
    @Autowired
    private Manager manager;

    public void storeHistoricMessage(...params...) {
        manager.storeHistoricMessage(...params...);
    }
}

我的应用程序上下文定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:component-scan base-package="package" />

    <bean id="JDBCPropertyConfigurer_service"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="JDBCProperties_service" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>

    <bean id="JDBCProperties_service"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager_service"/>

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

    <!-- datasource -->
    <bean id="dataSource_service"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database_service.driverClassName}" />
        <property name="url" value="${database_service.url}" />
        <property name="username" value="${database_service.username}" />
        <property name="password" value="${database_service.password}" />
    </bean>

    <!-- JPA config -->
    <bean id="entityManagerFactory_service"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource_service" />
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="Service" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!--
                    <property name="generateDdl" value="${jpa.generateDdl}" />
                    <property name="showSql" value="${jpa.showSql}" /> <property
                    name="hbm2ddl.auto" value="${jpa.hbm2ddl}" />
                -->
            </bean>
        </property>
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>

最后,我的persistence.xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="Service" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>package.HistoricMessage</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
            <property name="hibernate.archive.autodetection" value="class, hbm" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />
        </properties>
    </persistence-unit> 
</persistence>

问题在于,当我坚持时,没有任何反应:数据库中没有插入,没有错误消息,没有异常...... 我做了很多测试,也许还有一些帮助:

  • 如果我尝试用em.find()取回我的对象​​,我得到它...缓存优化?
  • 如果我捕获它,并在启动服务后重命名数据库中的表,我没有错误,他总是很高兴...
  • 如果我尝试em.flush(),它会抛出异常,因为没有正在进行的事务......

最后,我可以看到TRACE日志:

DEBUG 12-01 10:50:01,362 (SessionImpl.java:<init>:220)  -opened session at timestamp: 13263618013
DEBUG 12-01 10:50:01,362 (JDBCTransaction.java:begin:54)  -begin
DEBUG 12-01 10:50:01,362 (ConnectionManager.java:openConnection:421)  -opening JDBC connection
DEBUG 12-01 10:50:01,363 (DriverManagerDataSource.java:getConnectionFromDriver:163)  -Creating new JDBC DriverManager Connection to [jdbc:mysql://databaseurl]
DEBUG 12-01 10:50:01,378 (JDBCTransaction.java:begin:59)  -current autocommit status: true
DEBUG 12-01 10:50:01,379 (JDBCTransaction.java:begin:62)  -disabling autocommit
TRACE 12-01 10:50:01,380 (JDBCContext.java:afterTransactionBegin:214)  -after transaction begin
DEBUG 12-01 10:50:01,380 (JpaTransactionManager.java:doBegin:348)  -Exposing JPA transaction as JDBC transaction [SimpleConnectionHandle: com.mysql.jdbc.JDBC4Connection@1ff335bb]
TRACE 12-01 10:50:01,381 (TransactionSynchronizationManager.java:bindResource:186)  -Bound value [org.springframework.jdbc.datasource.ConnectionHolder@184c9860] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@65499154] to thread [http-8001-Processor5]
TRACE 12-01 10:50:01,381 (TransactionSynchronizationManager.java:bindResource:186)  -Bound value [org.springframework.orm.jpa.EntityManagerHolder@4ca7d316] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@28ce2c57] to thread [http-8001-Processor5]
TRACE 12-01 10:50:01,381 (TransactionSynchronizationManager.java:initSynchronization:261)  -Initializing transaction synchronization
TRACE 12-01 10:50:01,382 (TransactionAspectSupport.java:prepareTransactionInfo:290)  -Getting transaction for [package.storeHistoricMessage]
storeHistoricMessage (begin of my function)
DEBUG 12-01 10:50:01,382 (EntityManagerFactoryUtils.java:doGetTransactionalEntityManager:194)  -Opening JPA EntityManager
DEBUG 12-01 10:50:01,388 (EntityManagerFactoryUtils.java:doGetTransactionalEntityManager:199)  -Registering transaction synchronization for JPA EntityManager
DEBUG 12-01 10:50:01,389 (SessionImpl.java:<init>:220)  -opened session at timestamp: 13263618013
TRACE 12-01 10:50:01,392 (TransactionSynchronizationManager.java:bindResource:186)  -Bound value [org.springframework.orm.jpa.EntityManagerHolder@69851576] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@20442c19] to thread [http-8001-Processor5]
TRACE 12-01 10:50:01,393 (IdentifierValue.java:isUnsaved:77)  -id unsaved-value strategy UNDEFINED
TRACE 12-01 10:50:01,394 (AbstractSaveEventListener.java:getEntityState:514)  -transient instance of: package.HistoricMessage
TRACE 12-01 10:50:01,394 (DefaultPersistEventListener.java:entityIsTransient:124)  -saving transient instance
DEBUG 12-01 10:50:01,397 (AbstractSaveEventListener.java:saveWithGeneratedId:112)  -generated identifier: ***identifier***, using strategy: org.hibernate.id.Assigned
TRACE 12-01 10:50:01,397 (AbstractSaveEventListener.java:performSave:153)  -saving [package.HistoricMessage#***identifier***]
end of my function
TRACE 12-01 10:50:01,399 (TransactionAspectSupport.java:commitTransactionAfterReturning:319)  -Completing transaction for [package.storeHistoricMessage]
TRACE 12-01 10:50:01,400 (AbstractPlatformTransactionManager.java:triggerBeforeCommit:903)  -Triggering beforeCommit synchronization
TRACE 12-01 10:50:01,400 (AbstractPlatformTransactionManager.java:triggerBeforeCompletion:916)  -Triggering beforeCompletion synchronization
TRACE 12-01 10:50:01,401 (TransactionSynchronizationManager.java:doUnbindResource:232)  -Removed value [org.springframework.orm.jpa.EntityManagerHolder@69851576] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@20442c19] from thread [http-8001-Processor5]
DEBUG 12-01 10:50:01,401 (EntityManagerFactoryUtils.java:closeEntityManager:313)  -Closing JPA EntityManager
TRACE 12-01 10:50:01,401 (SessionImpl.java:close:273)  -closing session
TRACE 12-01 10:50:01,402 (ConnectionManager.java:cleanup:375)  -connection already null in cleanup : no action
DEBUG 12-01 10:50:01,402 (AbstractPlatformTransactionManager.java:processCommit:730)  -Initiating transaction commit
DEBUG 12-01 10:50:01,402 (JpaTransactionManager.java:doCommit:451)  -Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@27b71c12]
DEBUG 12-01 10:50:01,403 (JDBCTransaction.java:commit:103)  -commit
TRACE 12-01 10:50:01,403 (SessionImpl.java:managedFlush:337)  -automatically flushing session
TRACE 12-01 10:50:01,403 (JDBCContext.java:beforeTransactionCompletion:205)  -before transaction completion
TRACE 12-01 10:50:01,404 (SessionImpl.java:beforeTransactionCompletion:393)  -before transaction completion
DEBUG 12-01 10:50:01,405 (JDBCTransaction.java:toggleAutoCommit:193)  -re-enabling autocommit
DEBUG 12-01 10:50:01,406 (JDBCTransaction.java:commit:116)  -committed JDBC Connection
TRACE 12-01 10:50:01,407 (JDBCContext.java:afterTransactionCompletion:219)  -after transaction completion
DEBUG 12-01 10:50:01,407 (ConnectionManager.java:aggressiveRelease:404)  -aggressively releasing JDBC connection
DEBUG 12-01 10:50:01,408 (ConnectionManager.java:closeConnection:441)  -releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
TRACE 12-01 10:50:01,408 (SessionImpl.java:afterTransactionCompletion:422)  -after transaction completion
TRACE 12-01 10:50:01,409 (AbstractPlatformTransactionManager.java:triggerAfterCommit:929)  -Triggering afterCommit synchronization
TRACE 12-01 10:50:01,409 (AbstractPlatformTransactionManager.java:triggerAfterCompletion:945)  -Triggering afterCompletion synchronization
TRACE 12-01 10:50:01,410 (TransactionSynchronizationManager.java:clearSynchronization:315)  -Clearing transaction synchronization
TRACE 12-01 10:50:01,410 (TransactionSynchronizationManager.java:doUnbindResource:232)  -Removed value [org.springframework.orm.jpa.EntityManagerHolder@4ca7d316] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@28ce2c57] from thread [http-8001-Processor5]
TRACE 12-01 10:50:01,410 (TransactionSynchronizationManager.java:doUnbindResource:232)  -Removed value [org.springframework.jdbc.datasource.ConnectionHolder@184c9860] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@65499154] from thread [http-8001-Processor5]
DEBUG 12-01 10:50:01,411 (JpaTransactionManager.java:doCleanupAfterCompletion:534)  -Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@27b71c12] after transaction
DEBUG 12-01 10:50:01,411 (EntityManagerFactoryUtils.java:closeEntityManager:313)  -Closing JPA EntityManager
TRACE 12-01 10:50:01,411 (SessionImpl.java:close:273)  -closing session
TRACE 12-01 10:50:01,412 (ConnectionManager.java:cleanup:375)  -connection already null in cleanup : no action

非常感谢你的帮助!

编辑: 我很抱歉,我理解发生了什么,你无法找到,因为我真的简化了我的项目的架构,我没有告诉你原因: 在这个项目中,我有几个应用程序上下文文件和几个数据源,所以我使用了几次....而对于Spring 2.5,你只能有一个! 所以我不能在我的第二次服务中使用交易...... 为了解决这个问题,我使用了这样的:

<tx:advice id="txAdviceSelfCare" transaction-manager="transactionManager_service">
    <!-- the transactional semantics... -->
    <tx:attributes>
        <!-- other methods use the default transaction settings (see below) -->
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="ServiceManagerOperations"
        expression="execution(* package.ServiceManager.*(..))" />
    <aop:advisor advice-ref="txAdviceService"
        pointcut-ref="ServiceManagerOperations" />
</aop:config>

<!-- a PlatformTransactionManager is still required -->
<bean id="transactionManager_service" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory_service" />
</bean>

感谢大家的帮助!

5 个答案:

答案 0 :(得分:1)

你可以尝试检查数据库上真正发生的事情吗? 尝试激活该数据库上的日志工具,如果安装了这样的工具来嗅探Java代码和数据库服务器之间的网络流量...它可能很有用...... 您可以要求您的JPA图层显示生成的SQL,它可以为您提供有趣的数据...... 我的2件 杰罗姆

答案 1 :(得分:0)

更改catch块以执行某些操作并观察其打印内容。

    try {
        em.persist(historicMessage);
    }
    catch (Exception e){
        e.printStackTrace(); // use a logger if available
    }

您还可以将以下属性更改为true以查看正在进行的操作。

<property name="hibernate.show_sql" value="true" />

此外,养成使用合适的日志框架(例如SLF4J)跟踪方法进入/退出的习惯。

答案 2 :(得分:0)

请注意,实际的插入不会发生在em.persist语句中,因为调用是事务性的,所以你的try / catch根本不会做太多。实际的持久化是在事务结束时完成的,即服务层调用完成时(从方法返回后)。调试持久性时,这些知识至关重要。

答案 3 :(得分:0)

验证数据库中您查看详细信息的位置。日志清楚地表明提交已完成..

DEBUG 12-01 10:50:01,403 (JDBCTransaction.java:commit:103) -commit TRACE 12-01 10:50:01,403 (SessionImpl.java:managedFlush:337) -automatically flushing session TRACE 12-01 10:50:01,403 (JDBCContext.java:beforeTransactionCompletion:205) -before transaction completion TRACE 12-01 10:50:01,404 (SessionImpl.java:beforeTransactionCompletion:393) -before transaction completion DEBUG 12-01 10:50:01,405 (JDBCTransaction.java:toggleAutoCommit:193) -re-enabling autocommit DEBUG 12-01 10:50:01,406 (JDBCTransaction.java:commit:116) -committed JDBC Connection

答案 4 :(得分:0)

将您的方法调用放在try catch

public void storeHistoricMessage(...params...) {
    try {
       manager.storeHistoricMessage(...params...);
    } catch (Exception e) {
       e.printStackTrace(); // use a logger if available
    }
}

数据应该在manager.storeHistoricMessage方法调用(由事务管理代理)调用结束时保留,而不是在em.persist调用。你也可以试试em.flush:

try {
        em.persist(historicMessage);
        em.flush(); //force DB insert
    }...

看起来你并没有看到一个例外,可能是由交易经理抛出的。