Spring - Hibernate => Transactions
Hello,
I begin with Spring 3.1.1/Hibernate 4.1.0/Struts 2.2.3.
I have no error in logs, when i select data all is ok but when i save an object, i have always no error but the object is not save in the mysql database. I think transaction is never commit.
This is my applicationContext :
Code:
<?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>
This is applicationContext-ccr-hibernate.xml (in a module) :
Code:
<?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>
My GenericDaoImpl.java :
Code:
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 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;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
My implementationof Dao RessourceImplDao.java :
Code:
@Repository
public class RessourceImplDao extends GenericDAOImpl<RessourceImpl, Integer> {
public RessourceImplDao() {
super(RessourceImpl.class);
}
}
My service RessourceImpService.java that i made in "Transactional":
Code:
@Service
@Transactional
public class RessourceImplService {
@Autowired
private RessourceImplDao ressourceImplDao;
public void enregistrerRessource(RessourceImpl r) {
ressourceImplDao.save(r);
}
}
Thanks for you help!