Announcement Announcement Module
Collapse
No announcement yet.
Transactions not running in a bean created via a factory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transactions not running in a bean created via a factory

    I have a factory which is a static class, instantiated via Spring

    Code:
    @Component
    public class CommunicationFactory implements BeanFactoryAware
    {
    	// Main DAO
    	private static CommunicationDAO communicationDAO;
    
    	@Autowired(required = true)
    	public void setCommunicationDAO(CommunicationDAO communicationDAO)
    	{
    		CommunicationFactory.communicationDAO = communicationDAO;
    	}
    
    	private static BeanFactory beanFactory;
    
    	@Override
    	public void setBeanFactory(BeanFactory beanFactory) throws BeansException
    	{
    		CommunicationFactory.beanFactory = beanFactory;
    	}
    	
    	public static Email createEmail()
    	{
    		Email email = (Email)beanFactory.getBean("emailBean");
    		
    		// Set the DAO reference
    		email.setCommunicationDAO(communicationDAO);
    
    		return email;
    	}
    }
    Within the Email class I have a save() method which uses the DAO to save the email. When I annotate the save method with @Transactional I expect that the transactions will run, since the Email object was created through Spring. However, I am finding that the transactions are not running. As part of the save a delete occurs on one piece of data, and a SQLException occurs later but the deleted data is still gone. Nothing was rolled back.

    I have added excessive logging and do not see anything about transactions being created. I have also scoured google, books, stackoverflow, forums etc... and have not found an answer. I feel like I am missing something obvious. Are there any other requirements for transactions or am I missing something else?

    P.S. I do see transaction logs when I run the same method via a JUnit test and the error is rolled back. I also have transactions working in other beans which are directly created by spring.

  • #2
    Hi
    For using @Transactional annotation in Spring,
    you need to enable this feature, by including a
    snnipet like following, in your bean configuration file:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    default-lazy-init="false" default-autowire="no" default-dependency-check="none">

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


    hope it is helpful for you.

    Comment


    • #3
      Hi - thanks for the reply. As mentioned though I do have all the Spring transactional stuff setup as the transactions do work through a unit test. I also have them working through other places in the code using the annotation.

      I originally did not have things setup with getBean and was just using 'new' so when I realized that I thought, "oh that's my problem." However, even with the use of getBean() where Spring creates this bean I do not get transactions happening.

      Comment


      • #4
        Can anyone offer any advice on this issue?

        Here is a bit more background. Hopefully someone can provide a clue

        The process goes like this
        • A JSP is accessed. Inside that JSP it calls EmailFactory.createEmail()
        • The EmailFactory (which was created by Spring is a static class (everything inside is static: methods & variables)) uses BeanFactoryAware/BeanFactory to create a new instance of the Email class
        • The JSP calls a method on the instance of Email it has called save()
        • save() is marked as @Transactional
        • The save() method does multiple changes in the database, including some deletes. If one of the latter calls I am expecting the whole transaction to be rolled back
        • What is happening is that the database records that are deleted are actually still gone. i.e. the transaction was not rolled back

        Does this have something to do with the JSP? Is it not true that any bean created via Spring would have its @Transactional methods honoured?

        Comment

        Working...
        X