Announcement Announcement Module
Collapse
No announcement yet.
Why tx:annotation-driven doesn't work in my service configuration file? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why tx:annotation-driven doesn't work in my service configuration file?

    Hello I need some feedback about one problem.

    I use spring mvc with hibernate.
    For now I have 2 contexts:

    - the service context (which is loaded in the root web application) configured by the file persistence-context.xml

    - the spring mvc context (DispatcherServlet web application) configured by the file servletname-servlet.xml

    I used the annotation for the transaction and doesnt work when I put <tx:annotation-driven transaction-manager="transactionManager"/> in the service layer configuration file

    But it works when I put <tx:annotation-driven transaction-manager="transactionManager"/> in the dispatcher servlet configuration file!

    I though it is maybe because the transaction propagate all the way up to the controller...

    I know it is a bad practise, so my question is why this happens? How to fix it?
    Is it a problem of propagation?

    I give you all information about my application

    The controller which uses my service:

    Code:
    public class WelcomeController implements Controller {
    
    	@Autowired
    	private SourceService service;
    	
    	private Logger log = Logger.getLogger(this.getClass());
    	
    	private List<Hello> sources;
    	
    	@Override
    	public ModelAndView handleRequest(HttpServletRequest arg0,
    			HttpServletResponse arg1) throws Exception {
    		sources = service.findall();
    		Map<String, Object> map = new HashMap<String, Object>();
    		
    		map.put("sources", sources);
    		
    		log.info("sources size " + sources.size());
    		
    		
    		return new ModelAndView("welcome", map);
    	}
    
    }
    The service class with the transaction, it will use DAO inside

    Code:
    @Service("MonService")
    @Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=true)
    public class SourceServiceImpl implements SourceService {
    
    	@Autowired
    	private SourceDao dao;
    	
    	@Override
    	public List<Hello> findall() {
    		// TODO Auto-generated method stub
    		return dao.findall();
    	}
    
    }
    I show you all my configuration files

    web.xml file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>NewsTranslation</display-name>
      
       <listener>
    	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/persistence-context.xml</param-value>
    	</context-param>
    	
      	<servlet>
    	<description>Spring MVC Dispatcher Servlet</description>
    	<servlet-name>newstranslation</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	<load-on-startup>2</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    	<servlet-name>newstranslation</servlet-name>
    	<url-pattern>*.htm</url-pattern>
    	</servlet-mapping>
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    The service configuration files

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jee="http://www.springframework.org/schema/jee"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    
    	<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="url" value="jdbc:mysql://localhost/test" />
    		<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
    		<property name="username"  value="irnbru"/>
    		<property name="password" value="motdepasse"/>
    	</bean>
    
    	<bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="datasource"/>
    		<property name="annotatedClasses">
    			<list>
           			<value>net.developpez.irnbru.tutorial.domain.Hello</value>
         		</list>				
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
    				<prop key="hibernate.show.sql">true</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="MonHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory" ref="sessionfactory"/>
    	</bean>
    	
    	<bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionfactory"></property>
    	</bean>
    	
    	
    </beans>
    The DispatcherServlet configuration file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jee="http://www.springframework.org/schema/jee"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    
    	<bean name="/welcome.htm" class="net.developpez.irnbru.tutorial.controller.WelcomeController"/>
    	
    	<bean id="viewResolvernico" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/jsp/"/>
    		<property name="suffix" value=".jsp"/>
    	</bean>
    	
    	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename" value="messages"></property>
    	</bean>
    	
    	<context:annotation-config/>
    	<context:component-scan base-package="net.developpez.irnbru.tutorial"/>
    	
    	 <bean id="localeResolver"
           class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
           <property name="cookieName" value="locale" />
           <property name="cookieMaxAge" value="30" />
           <property name="defaultLocale" value="en" />
        </bean>
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>
    See I had to put <tx:annotation-driven transaction-manager="transactionManager"/> in the wrong file.

    Please help me

    Thanks
    IRNBRU
    Last edited by irnbru; Apr 27th, 2010, 05:58 AM.

  • #2
    I suggest the forum as this question has been answered numerous times before.

    You have 2 instances of your service due to component-scanning. Configure your component scanning to only include what you need, for your DispatcherServlet that is only @Controllers probably. YOur services should be loaded by the ContextLoaderListener so put the tx:annotation-driven there together with a component-scan that scans for @Service/@Repository.
    Last edited by Marten Deinum; Apr 27th, 2010, 07:40 AM. Reason: Should read closer

    Comment


    • #3
      hi Marten Deinum thank you very much for your very precious help

      It worked better! But I didn't quite understand this. Could you explain me more what happened? Can you tell me where to find info in the forum about the topics you mentioned

      I suggest the forum as this question has been answered numerous times before.
      Where can I find them?

      It seems there is a relationship between <context:component-scan base-package=""/> and <tx:annotation-driven transaction-manager="transactionManager"/> but which one I don't know yet!

      For All your information here what I did:

      For the service layer:

      Code:
              <context:annotation-config/>
      	
              <context:component-scan base-package="net.developpez.irnbru.tutorial.service"/>
      	
              <context:component-scan base-package="net.developpez.irnbru.tutorial.dao"/>
      	
      	<tx:annotation-driven transaction-manager="transactionManager"/>
      For the presentation layer:

      Code:
             <context:component-scan base-package="net.developpez.irnbru.tutorial.controller"/>
      I added this so my service can be injected inside the controller (through the @Autowired annotation). if I didn't add <context:component-scan base-package="net.developpez.irnbru.tutorial.controller "/> I get back a null pointer instead of my dear love service!

      At least the tag tx:annotation-driven is in the right place

      Thanks Marten Deinum you are a pro and showed me the way!

      IRNBRU

      Comment


      • #4
        all post are very nice. thanks for sharing...........
        Last edited by jontyrodhes; Apr 28th, 2010, 02:00 AM.

        Comment


        • #5
          There is not really a tight dependency.

          component-scan scan the given package for @Components and registers them as spring beans. tx annotation driven scans registered spring beans for @Transactional. You can perfectly use component-scan without tx annotation driven and vice versa.

          Comment


          • #6
            Thanks Marten Deinum I knew this, but what surprised me was when I changed the package of the component scanning, it influenced the transaction part. Before I solved my problem the tx tag was ignored when I put it in the service configuration file. But once I tightened the scope of the component scanning (with more precise package in base-package) Spring took notice of the tx tag.

            So while the component-scan and tx annotations are not tightly linked, there is a cause and effect all the same, obviously! otherwise changing the base-package, like I did, would have been pointless. I used a base-package which was too wide, and it can do unexpected results....

            Thanks again for the clue you gave me!
            IRNBRU

            Comment

            Working...
            X