简体   繁体   English

Java和Spring实现事务功能

[英]Java and Spring implement transactional function

I'm using java 1.6 and spring 3.0.4, I want to realize a java functionality that 我正在使用Java 1.6和Spring 3.0.4,我想实现一个Java功能

  • calculate new data values 计算新数据值
  • update one-by-one the existing values on the database 一对一更新数据库中的现有值

If in any of this step there's an error I want to rollback the whole transaction and come back to the previous state. 如果在任何此步骤中出现错误,我都希望回滚整个事务并返回到先前的状态。

I already realized all this pieces of code, I just want to put them together. 我已经意识到了所有这些代码,我只想将它们放在一起。 How I can manage this with the existing spring values that are working with @Entity and @Column annotations? 如何使用与@Entity@Column批注一起使用的现有spring值来管理此问题?

Thanks! 谢谢!

Short answer: as you're using Spring, the easiest would be to use the transaction management , creating a service that represents this transaction unit and annotate the method with @Transactional 简短的答案:使用Spring时,最简单的方法是使用事务管理 ,创建一个代表该事务单元的服务,并使用@Transactional对该方法进行注释

In practice, you need to configure a PlatformTransactionManager in your application. 实际上,您需要在应用程序中配置PlatformTransactionManager As you seem to use JPA, the JpaTransationManager seems like an obvious choice. 当您似乎使用JPA时, JpaTransationManager似乎是一个显而易见的选择。 To enable the processing of the @Transactional annotation, you can either use @EnableTransactionManagement or the <tx:annotation-driven/> namespace. 要启用@Transactional批注的处理,可以使用@EnableTransactionManagement<tx:annotation-driven/>命名空间。 Both are explained in the Javadoc of @EnableTransactionManagement 两者都在@EnableTransactionManagement的Javadoc中进行了@EnableTransactionManagement

By default, a runtime exception thrown from that annotated method will manage a transaction rollback. 默认情况下,从该带注释的方法引发的运行时异常将管理事务回滚。 If your code is using checked exceptions, you'll have to configure the rollbackFor attribute of the annotation. 如果您的代码正在使用检查的异常,则必须配置注释的rollbackFor属性。

There are more details and examples available in the documentation 文档中有更多详细信息和示例

For people that need the same configuration, here you can find how I solved this problem, integrating Hibernate with Spring. 对于需要相同配置的人们,您可以在这里找到我如何解决此问题,将Hibernate与Spring集成在一起。

<!-- session factory activate the transaction modules for the specified classes -->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
  <ref bean="dataSource" />
</property>
<property name="configLocation" value="classpath:config-hibernate.xml" />
<property name="packagesToScan">
  <list>
    <!-- Additional packages required to be added if entities located elsewhere -->
    <value>com.some.package.dao</value>
    <value>com.some.package.model</value>
    <value>com.some.package.SpecificClass</value>
  </list>
</property>
<property name="mappingResources" ref="mappingResources"/>

<bean id="mappingResources" class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
  <list>
    <!-- here you can add your hibernate mapping query that you want to use on transaction -->
    <value>config-hibernate-mapping.xml</value>
  </list>
 </property>
</bean>

<!-- This will activate transactional annotation -->
<tx:annotation-driven transaction-manager="transactionManager" />

@Service
@Transactional
public class SpecificClass {
  // write your method, everyone of them will be transactional
  // and there will be  a commit in case of success or rollback in case of exception
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM