Closed spring-projects-issues closed 10 years ago
Yuwei Zhao commented
I run to similar problem. I list everything I did in detail here.
create database sptestDB character set utf8;
grant select, insert, update, delete on sptestDB.* to sp@localhost identified by 'sp';
use sptestDB;
create table data (id int unsigned not null auto_increment, name varchar(255) not null, primary key(id)) engine = innodb;
\<?xml version="1.0" encoding="UTF-8"?>
\<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
\
\
\<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
\
\
\<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
\
\<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
\
Yuwei Zhao commented
Run ant-task dist, it will build and package exploded war in dist.war directory.
David Mas commented
I experiment the same issue: I have a generic crud controller, and the concrete class is annotated with @Controller
. The abstract generic crud controlled has its methods annotated with @Transactional
, and at runtime there are no calls to TransactionInterceptor#invoke.
Lucas commented
Same problem here. I'm using spring roo, I just added a class in the scanned package, annotated it as Component and Transactional, added a method that persists an Object using JPA, it just doesn't work, no exception, the object is just not saved, the id is not assigned, if I flush the object, I get
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'systemInitializer': Invocation of init method failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
I tried replacing @Component
with @Service
and @Repository
, same result.
If a I declare the bean in the xml config file, and delet the @Component
annotation, it works.
Marcin Muras commented
I had same problem but after added proxy-target-class="true" to <tx:annotation-driven .. everything works again. Please check this solution.
Julien Dubois commented
Hi, You just messed up your component-scanning: your "Service" bean is in fact declared both in your applicationContext.xml file, and your servlet.xml file. So your controller uses the one from the servlet.xml file (it's in the same Web child application context to be precise). As your transactionnal aspect is only defined in your applicationContext.xml file (the parent application context), you don't have transactions in the bean instance you use. When you switch to XML, you only declare your bean once, in the (correct) applicationContext.xml file -> so this time, the controller uses this instance, which is transactionnal.
To put it simple:
1.move your service beans to a com.xml.service package,
2.use
You should have a look at the Spring debug logs, it will show you what happens.
Stéphane Nicoll commented
another solution is to also enable
Yuwei Zhao commented
Reply to Julien Dubois:
You just messed up your component-scanning: your "Service" bean is in fact declared both in your applicationContext.xml file, and your servlet.xml file.
I have include-filter specified in my examples,
In applicationContext.xml:
Rohit Kanchan commented
@Yuwei
Zhao.. r u able to resolve this? I am facing the same issue with Spring 3.1. I have tried different things explained here, nothing worked for me. I have removed @Service
from service layer and added into the servlet xml file, still I am getting following exception. Please let me know if any one of u got solution of this. Thanks in advance.
javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManag erImpl.java:978) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEnti
Yuwei Zhao commented
@Rohit
Kanchan
By the book, service components shall be put into applicationContext.xml, not servlet.xml, unless you only have one IoC Container.
In my experience, if @Service
annotation is removed, and \
Gleb Schukin commented
I had the same issue. Adding mode="proxy" to tx:annotation-driven fixed it.
Julien Dubois commented
Yuwei : read my comment again. This is not how include-filter work (have a look at use-default-filters="false").
This is definitely not a bug, just a configuration error.
But I agree this configuration is complex, I often get questions about this, and this is a huge source of mistakes.
My recommandation : use the simplest configuration possible, as I have in my comment, unless you really need something special.
Yuwei Zhao opened SPR-5082 and commented
Please see the following example:
And everything supports the annotation is configurated:
We use
@Service
annotation on service bean, that is, it could be autowired to any bean that depends on it.But the
@Transactional
annotation doesn't work, until I remove@Service
annotation and explicitly define the bean in context configuration xml:I'm wondering if there is any step I missed, or it supposes to work like that? Thanks.
Affects: 2.5 final
Attachments:
Issue Links:
10953 Autowired can not work with
@Transactional
10 votes, 16 watchers