简体   繁体   中英

Spring transaction annotation not working

I'm using Spring 3.2.4 with JavaFX and wanted to implement a method, where the operations will be performed in a transaction. My code in the controller looks like this:

@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);
    }

And my application-context:

<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>

Trying to run this, I get this error message:

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)
  ...

The method exists. With either removing the annotation, or changing the method from public to private or removing the bean from the config, the program runs but the @Transactional -annotation simply would not work. Removing proxy-target-class="true" leads me to another error.

If you are talking about @Transactional then it is used to rollback the Current Transaction Happen.

That is mostly used in Making Test Cases, Let me show you :

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
}

now here in @TransactionConfiguration it takes transactionManager from here, an XML file :

<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" />

it will automatically get txManager bean.

Still if any query POST me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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