@Transactional et le problème 'no transaction in progress'
Bonjour,
je suis au début d'un projet utilisant Spring 2.5 hibernate JPA, pour gérer les transactions je le fais avec le @Transactional de Spring.
le fichier applicationContext.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/base" />
<property name="properties">
<props>
<prop key="c3p0.acquire_increment">5</prop>
<prop key="c3p0.idle_test_period">100</prop>
<prop key="c3p0.max_size">100</prop>
<prop key="c3p0.max_statements">0</prop>
<prop key="c3p0.min_size">10</prop>
<prop key="user">root</prop>
<prop key="password">pass</prop>
</props>
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="tiles"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform"
value="org.hibernate.dialect.SQLServerDialect"/>
<property name="generateDdl" value="false"/>
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<context:annotation-config />
<context:component-scan base-package="package" />
<aop:aspectj-autoproxy />
</beans> |
controlleur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
@Controller
@SessionAttributes(value = "produit")
public class ProduitController {
private IProduitService produitService;
public IProduitService getProduitService() {
return produitService;
}
@Autowired
public void setProduitService(IProduitService produitService) {
this.produitService = produitService;
}
@RequestMapping(value = "/produit/add.htm", method = RequestMethod.POST)
public String addProduit(@ModelAttribute("produit") Produit produit,
SessionStatus status) {
produitService.createProduct(produit);
status.setComplete();
return Constants.MENU_PRODUCTS;
}
... |
service:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@Service
@Transactional
public class ProduitServiceImpl implements IProduitService {
@Autowired
private ProduitDao produitDao;
public ProduitDao getProduitDao() {
return produitDao;
}
public void setProduitDao(ProduitDao produitDao) {
this.produitDao = produitDao;
}
public void createProduct(Produit produit) {
produitDao.persist(produit);
}
... |
le problème c'est avec un simple main ça marche nickel j'arrive à persister:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"META-INF/applicationContext.xml");
IProduitService produitService = (IProduitService)context.getBean("produitServiceImpl");
Produit produit = new Produit();
produit.setDescription("description");
produit.setPrice(21.21);
produitService.createProduct(produit);
} |
mais en web Spring n'arrive pas à gerer les transaction ie
Code:
1 2 3 4 5 6
|
DEBUG - delaying identity-insert due to no transaction in progress
DEBUG - processing cascade ACTION_MERGE for:...
DEBUG - done processing cascade ACTION_MERGE for: ...
DEBUG - Closing JPA EntityManager
DEBUG - closing session |
pour moi la seul difference entre le web et le main c'est qu'en web spring utilise
Code:
DEBUG - Publishing event in context [org.springframework.web.context.support.XmlWebApplicationContext@b80f1c].....
alors qu'en main moi j'ai utilisé
Code:
org.springframework.context.support.ClassPathXmlApplicationContext
Si quelqu'un peut m'eclaircir le problème ça sera vraiment très sympa:D car je bute sur cela depuis des jours.
Merci par avance de vos suggéstions et désolé si j'étais long:aie: