Pregunta

I need some help. I'm have a problem with a transaction with hibernate and spring. I'm trying to fill up a File table in mysql. My first insert works great, the second doesn't work (but it's normal...). But data from the frist insert are still present in the table. This doesn't fit the concept of transaction. Am i alright on this ?

I think a rollback should be executed the second time i try to insert corrupted data in database ( by corrupted data, i mean data that doesn't match field constraint) My first insert is ok but as the second insert is "corrupted" the first insert should be rollback and no data should be present in the table. Or it's not the case. First data , from the first insert are still in the table. It shouldn't be still present?

I tried to look checked /uncheked exception stuff, misconfiguration of @transactionnal without success...

If you have any idea...

Thanks !!

Main.java :

    public static void main( String[] args ) throws Exception{

            ApplicationContext appContext = new ClassPathXmlApplicationContext(    "classpath:webConfiguration/applicationContext.xml");

            FileBo aFileBo = (FileBo) appContext.getBean("fileBo");
// Data are ok, there is an insert.
File aFile = File (6778687,".bam");             
aFileBo.save(aFile);

// Error is produce here because the ".pdf" value is not tolerated in the enum field we want to fill up. No data is inserted. But a rollback should be done and data inserted before should be erased ?
File anAnotherFile = File (6567887,".pdf");             
aFileBo.save(anAnotherFile);

}

FileBoImpl.java

public class FileBoImpl implements FileBo{
FileDao fileDao;


public FileDao getFileDao() {
    return fileDao;
}


public void setFileDao(FileDao fileDao) {
    this.fileDao = fileDao;
}

@Transactional// one transaction for multiple operations
public void save(com.clb.genomic.lyon.model.File aFile) {

 fileDao.save(aFile);
}

Hibernate.xml:

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd " >

<!-- Hibernate session factory -->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource">
  <ref bean="dataSource"/>
</property>

<property name="hibernateProperties">
   <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
     <prop key="hibernate.show_sql">true</prop>
   </props>
 </property>

 <property name="mappingResources">
   ...
  </property>

</bean>

 <tx:annotation-driven transaction-manager="transactionManager" /> 

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>

</bean> 

Beans.xml called in applicationContext.xml with also Hibernate.xml :

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">


<!--  Data Access Object -->

<bean id="fileDao" class="com.clb.genomic.lyon.dao.FileDaoImpl" >
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean> 

<!--  Business Object  -->

<bean id="fileBo" class="com.clb.genomic.lyon.bo.FileBoImpl" >
    <property name="fileDao" ref="fileDao"></property>
</bean> 

</beans>
¿Fue útil?

Solución

You're using two separate transactions here: one to save the first file, and a second one to save the second file. So the first one is already committed when the second one rollbacks.

If you want the two saves to be part of the same transaction, then you should call a single transaction method from main, and save the two files from this method:

public static void main( String[] args ) throws Exception{
    ApplicationContext appContext = new ClassPathXmlApplicationContext(    "classpath:webConfiguration/applicationContext.xml");

    FileBo aFileBo = (FileBo) appContext.getBean("fileBo");
    // Data are ok, there is an insert.
    File aFile = File (6778687,".bam");    
    File anAnotherFile = File (6567887,".pdf");  

    aFileBo.save(aFile, anotherFile);
}

public class FileBoImpl implements FileBo {
    FileDao fileDao;
    // ...

    @Transactional
    public void save(com.clb.genomic.lyon.model.File aFile, 
                     com.clb.genomic.lyon.model.File bFile) {

        fileDao.save(aFile);
        fileDao.save(bFile);

    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top