Spring Hibernate事务提交但不保存在DB中

时间:2018-03-20 07:37:09

标签: spring hibernate spring-transactions

使用Spring Boot,声明式事务管理。

查找操作工作正常,但保存不反映数据库中的更改。 如果需要更多信息,请告诉我。

this.getCurrentSession().saveOrUpdate(entity);

Spring Boot配置:

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class,
                              DataSourceAutoConfiguration.class,
                              DataSourceTransactionManagerAutoConfiguration.class,
                              HibernateJpaAutoConfiguration.class,
                              JdbcTemplateAutoConfiguration.class,
                              TransactionAutoConfiguration.class,
                              WebSocketAutoConfiguration.class})

RDBM持久性bean定义:

<bean name="transactionAgnosticDataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
    <property name="url" value="${spring.datasource.url}"/>
    <property name="username" value="${spring.datasource.username}"/>
    <property name="password" value="${spring.datasource.password}"/>
    <property name="driverClassName" value="org.mariadb.jdbc.Driver"/>
    <property name="jmxEnabled" value="false"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnConnect" value="true"/>
    <property name="testWhileIdle" value="false"/>
    <property name="testOnReturn" value="false"/>
    <property name="timeBetweenEvictionRunsMillis" value="30000"/>
    <property name="validationQuery" value="SELECT 1"/>
    <property name="validationQueryTimeout" value="5"/>
    <property name="validationInterval" value="30000"/>
    <property name="maxActive" value="50"/>
    <property name="maxIdle" value="30"/>
    <property name="initialSize" value="20"/>
</bean>

<bean name="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
    <property name="targetDataSource" ref="transactionAgnosticDataSource"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
        <list>
            <value>com.test.xyz.persistence.model.AlertOptImpl</value>
            <value>com.test.xyz.persistence.model.ChallengeImpl</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="find*" read-only="true" rollback-for="java.lang.Throwable"/>
        <tx:method name="*" rollback-for="java.lang.Throwable"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="serviceOperation" expression="execution(* com.test.xyz.core.service.*ServiceImpl.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>

服务方式:

@Override
public void test() {
    TestResource testResource = testResourceDao.findById(1l).get();
    testResource.setPosition(7);
    testResource.save(testResource); //this calls  this.getCurrentSession().saveOrUpdate(entity);
    //testResource.flushSession();
}

没有任何错误。在调试日志中注意到以下内容:

o.s.o.h.HibernateTransactionManager      : Initiating transaction commit
o.s.o.h.HibernateTransactionManager      : Committing Hibernate transaction on Session
o.s.jdbc.datasource.DataSourceUtils      : Resetting read-only flag of JDBC Connection [Transaction-aware proxy for target Connection
o.s.o.h.HibernateTransactionManager      : Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[EntityKey
o.s.jdbc.datasource.DataSourceUtils      : Returning JDBC Connection to DataSource
o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Written [[com.
o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
o.s.web.servlet.DispatcherServlet        : Successfully completed request

0 个答案:

没有答案