@Transactional not working with Spring MVC

2,389 views
Skip to first unread message

Reaves, Timothy

unread,
Dec 3, 2012, 8:48:25 AM12/3/12
to ne...@googlegroups.com
I'm having the issue described here: http://forum.springsource.org/showthread.php?122363-Applying-transaction-boundaries

I do have my autoscan correct, with:
    <!-- Auto scan the components, excluding the controllers (they are scanned in dispatchServlet-servlet.xml -->
    <context:component-scan base-package="com.my.base.package">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

and the one that has controllers elsewhere. Both of these statements are working, as my controllers are controllers, and the autowiring of repositories is all working (so both of the component-scan must be running).  But the @Transactional in my controllers is not working, so, I'm getting not in transaction exceptions.

Can anyone think of something I may have missed?

Michael Hunger

unread,
Dec 3, 2012, 8:50:56 AM12/3/12
to ne...@googlegroups.com
do you have

<tx:annotation-driven mode="proxy" or mode="aspectj"> ??

in your application-context ?

Michael

--
 
 

Reaves, Timothy

unread,
Dec 3, 2012, 9:32:43 AM12/3/12
to ne...@googlegroups.com
I forgot to mention that. 

I currently have it set to proxy (which is what the Neo4j docs have), but, I had tried the others listed in the thread I referenced.  I do not do any AspectJ configuration, as that thread was geared more towards testing too.


--
 
 

Michael Hunger

unread,
Dec 3, 2012, 10:04:46 AM12/3/12
to ne...@googlegroups.com
Transactional only applies to the services if you have the tx declaration in the other file and also the component scan.

If you also want to have transactions in the controllers you have to add a separate declaration in your dispatch-Servlet-servlet.xml

Michael

--
 
 

Reaves, Timothy

unread,
Dec 3, 2012, 10:27:52 AM12/3/12
to ne...@googlegroups.com
O.K. I can understand that; the docs don't mention this because they do not use the store in their controllers I guess. 

The component-scan was already there.  However, now with the tx added, my controlelrs ar eno longer being auto-wired, causing NPE's.  I'm not sure why adding transaction management to the servlet config file file would cause auto-wiring to fail, but, that seems like a defect.


--
 
 

Reaves, Timothy

unread,
Dec 3, 2012, 1:37:54 PM12/3/12
to ne...@googlegroups.com
So why would adding the component scan directive to the servlet xml file cause auto-wiring to not occur?

Reaves, Timothy

unread,
Dec 3, 2012, 2:01:35 PM12/3/12
to ne...@googlegroups.com
Oddly, using
    <tx:annotation-driven mode="aspectj" />
works in my servlet XML file, whereas
    <tx:annotation-driven mode="proxy" />
still causes auto-wiring to not happen.

When using transaction directives in more than one file, must each one specify a different mode?  What's going on with this?  Very odd.

Reaves, Timothy

unread,
Dec 3, 2012, 2:02:35 PM12/3/12
to ne...@googlegroups.com
I was not clear in my last post; using the aspectj causes the auto-wiring to occur, but, I still get not in transaction exceptions.

Reaves, Timothy

unread,
Dec 4, 2012, 8:48:02 AM12/4/12
to ne...@googlegroups.com
Why would adding transactional support to my servlet config XML cause auto wiring to not occur?

Michael Hunger

unread,
Dec 4, 2012, 10:54:55 AM12/4/12
to ne...@googlegroups.com
Timothy,

can you please share your full project setup (privately) from the fragments it is difficult to reason about it.

How many context-files do you have? One for the services, one for the controllers and the default-servlet one?

The tx: namespace must be in those that contain the component scan for the type of classes you want to have automatic tx-management on.

Do you also have <context:annotation-driven/> in there?

Michael

--
 
 

Reply all
Reply to author
Forward
0 new messages