Spring事务注释不起作用

时间:2014-06-10 02:27:20

标签: java spring transactions spring-transactions

我使用Spring 3.2.4和JavaFX并希望实现一种方法,其中操作将在事务中执行。我在控制器中的代码如下所示:

@Transactional(rollbackFor = { ServiceException.class,
            ValidationException.class })
    public void registerVolunteer(User user, Volunteer volunteer)
            throws ServiceException, ValidationException {
        User ret = userService.create(user);
        volunteer.setUser(ret);
        volunteerService.untransactedCreate(volunteer);
    }

我的应用程序上下文:

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    <!-- Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

尝试运行此命令,我收到以下错误消息:

Controller method "onBackClicked" not found.
/home/workspace/project/NewProject/target/classes/fxml/RegisterVolunteer.fxml:69
  at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:497)
  at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:594)
  at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2472)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2177)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2069)
  at org.impactvolunteers.management.FXMLSpringLoader.load(FXMLSpringLoader.java:97)
  at org.impactvolunteers.management.FXMLSpringLoader.load(FXMLSpringLoader.java:81)
  at org.impactvolunteers.management.gui.Screen.init(Screen.java:50)
  at org.impactvolunteers.management.gui.ScreensController.init(ScreensController.java:43)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  ...

该方法存在。无论是删除注释,还是将方法从public更改为private或从配置中删除bean,程序都会运行,但@Transactional - 注释根本不起作用。删除proxy-target-class="true"会导致另一个错误。

1 个答案:

答案 0 :(得分:1)

如果您正在谈论@Transactional,那么它将用于回滚当前事务发生。

这主要用于制作测试用例,让我告诉你:

RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/dispatcher-servlet.xml")
@TransactionConfiguration(transactionManager = "txManager",defaultRollback = true)
@Transactional
public class AddVendorProcessorTest{

   // Your code
}

现在在@TransactionConfiguration这里需要transactionManager,这是一个XML文件:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost:5432/db"/>
    <property name="username" value="postgres"/>
    <property name="password" value="pwd"/>
    <property name="validationQuery" value="SELECT 1"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.domain"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.generate_statistics">true</prop>
        </props>
    </property>
</bean>

<!-- Transaction Manager -->
<bean id="txManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

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

它将自动获得txManager bean。

如果有任何查询POST我。