Spring - Hibernate => Transactions
Bonjour,
Je débute avec Spring 3.1.1/Hibernate 4.1.0/Struts 2.2.3.
Tout a l'air de bien fonctionner sauf que mes transactions (si elle se lance bien?) ne sont en fait jamais commité dans ma base de données. L'insertion d'une ressource ce passe bien aucun erreur, mais l'enregistrement en base ne se fait pas.
Voici mon applicationContext :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<?xml version="1.0" encoding="ISO-8859-15"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Import de la config hibernate/dao depuis le ccr-core -->
<import resource="classpath:applicationContext-ccr-hibernate.xml"/>
<!-- Activation des annotations -->
<context:component-scan base-package="fr.cndp.ccr.core.dao"/>
<context:component-scan base-package="fr.cndp.ccr.core.service"/>
</beans> |
Mon applicationContext-ccr-hibernate.xml (qui se trouve dans un sous-module) :
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
<?xml version="1.0" encoding="ISO-8859-15"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSourceCCR"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://javadev.cndp.lan:3306/ccrdb" />
<!--<property name="url" value="jdbc:mysql://localhost:3306/ccrdb" />-->
<property name="username" value="db" />
<property name="password" value="toto" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSourceCCR" />
<property name="mappingLocations">
<value>classpath*:fr/cndp/ccr/core/modele/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.id.new_generator_mappings">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.validator.apply_to_ddl">false</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<!--<prop key="current_session_context_class">thread</prop>-->
<prop key="hibernate.validator.autoregister_listeners">false</prop>
<prop key="c3p0.acquire_increment">1</prop>
<prop key="c3p0.min_size">1</prop>
<prop key="c3p0.max_size">20</prop>
<prop key="c3p0.timeout">0</prop>
<prop key="c3p0.max_statements">0</prop>
<prop key="c3p0.idle_test_period">3600</prop>
</props>
</property>
</bean>
<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSourceCCR" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
<aop:config proxy-target-class="true"/>
</beans> |
Mon GenericDaoImpl.java :
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
package fr.cndp.ccr.core.dao;
public abstract class GenericDAOImpl<T, K extends Serializable> implements GenericDAO<T, K> {
protected static Logger logger;
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
private Session session;
private Class type;
public GenericDAOImpl(Class<T> leType) {
type = leType;
logger = Logger.getLogger(type);
}
/**
* Permet de recuperer un T selon son id K
* @param id
* @return
*/
@Override
public T find(K id) {
return (T) getSession().get(type, id);
}
/**
* Permet de recuperer la liste de tous les T
* @return
*/
@Override
public List<T> find() {
Criteria crit = getSession().createCriteria(type);
return crit.list();
}
/**
* Permet de recuper un ensemble de T en fonction d'un T exemple
* @param example
* @return
*/
@Override
public List<T> find(T example) {
return null;
}
/**
* Permet d'enregistrer en base un nouveau T
* @param instance
* @return
*/
@Override
public K save(T instance) {
K reponse = (K) getSession().save(instance);
return reponse;
}
public Session getSession() {
if (session == null) {
try {
session = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
session = sessionFactory.openSession();
}
}
return this.session;
//return SessionFactoryUtils.getSession(sessionFactory, allowCreate);
}
public void setSessionFactory(SessionFactory sessionFactory) {
System.out.println("setSessionFactory : " + sessionFactory.toString());
this.sessionFactory = sessionFactory;
}
} |
Mon implémentation du Dao RessourceImplDao.java :
Code:
1 2 3 4 5 6 7 8
|
@Repository
public class RessourceImplDao extends GenericDAOImpl<RessourceImpl, Integer> {
public RessourceImplDao() {
super(RessourceImpl.class);
}
} |
Mon service RessourceImpService.java que j'ai mis en Transactional, ce qui n'est visiblement pas suffisant :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
@Service
@Transactional
public class RessourceImplService {
@Autowired
private RessourceImplDao ressourceImplDao;
public void enregistrerRessource(RessourceImpl r) {
ressourceImplDao.save(r);
}
} |
Merci d'avance pour toute aide
Transactions les données ne s'ajoutent pas
Bonjour,
spring ne fait pas de insert ou update qu'apres la fermuture de la ssesion donc tu dois utiliser les transactions avec aop
mais il faut ajouter les jar dans votre lib (webinf/lib)
aspectjrt.jar
aspectjweaver.jar