冬眠4+;Spring 3.2+;交易管理器&x2B;Mysql中没有回滚
我需要一些帮助。我发现hibernate和spring的事务有问题。 我试图在mysql中填充一个文件表。我的第一次插入效果很好,第二次不起作用(但这很正常…)。但是第一次插入的数据仍然存在于表中。这不符合事务的概念。我在这方面还好吗 我认为在我第二次尝试在数据库中插入损坏的数据时应该执行回滚(所谓损坏的数据,我指的是不符合字段约束的数据) 我的第一次插入是正常的,但是由于第二次插入“损坏”,第一次插入应该回滚,并且表中不应该存在任何数据。 或者事实并非如此。第一次插入的第一个数据仍在表中。它不应该还存在 我试图查看已检查/未检查的异常内容,@transactionnal的错误配置,但没有成功 如果你有任何想法 谢谢 Main.java:冬眠4+;Spring 3.2+;交易管理器&x2B;Mysql中没有回滚,mysql,spring,rollback,spring-transactions,hibernate-4.x,Mysql,Spring,Rollback,Spring Transactions,Hibernate 4.x,我需要一些帮助。我发现hibernate和spring的事务有问题。 我试图在mysql中填充一个文件表。我的第一次插入效果很好,第二次不起作用(但这很正常…)。但是第一次插入的数据仍然存在于表中。这不符合事务的概念。我在这方面还好吗 我认为在我第二次尝试在数据库中插入损坏的数据时应该执行回滚(所谓损坏的数据,我指的是不符合字段约束的数据) 我的第一次插入是正常的,但是由于第二次插入“损坏”,第一次插入应该回滚,并且表中不应该存在任何数据。 或者事实并非如此。第一次插入的第一个数据仍在表中。它不
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>
org.hibernate.dialogue.mysqldialogue
真的
...
在applicationContext.xml中调用Beans.xml,同时使用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>
您在这里使用两个单独的事务:一个用于保存第一个文件,另一个用于保存第二个文件。因此,当第二个回滚时,第一个已提交 如果希望两个保存是同一事务的一部分,则应从main调用单个事务方法,并从该方法保存两个文件:
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);
}
}
好的,这对我来说很清楚:-)所以我想知道如何处理要在同一事务中保存的多个实体。在我的代码中,所有DaoEntite都有一个对应的BoEntitie(如我前面所示)。当实体具有关系(多对多、一对多等)时,它们是否都集成在同一事务中?想象一下,如果我执行Repertory bo.save()、fileBo.save()和rightsBo.save(),您就拥有类似以下内容的内容:Repertory->File(一对多关系)和File->File_Rights->Rights(多对多关系),它是否因为关系而发生在同一事务中?在使用Transactional注释的方法的调用中所做的一切都是单个事务的一部分。事情(几乎)就这么简单。服务层(您称之为BOs)的作用是提供与必须在单个事务中完成的事情相对应的事务方法。如果您的服务层只有save()方法,那么它不会向DAO层添加任何内容。它应该有像transferAmount()这样的方法,这些方法检查输入,从一个帐户中删除金额,将金额添加到另一个帐户,并保存审核日志,所有这些都在一个事务中完成。我仍然不知道,如果有3个表,那么3个POJO类,3个DAO。您希望在全局事务中保存3个表的数据。因此,您不会使用带有Transactionnal注释的save方法来设计3个BOs(服务)类。我是否需要使用transactionnal方法实现1个服务类来保存所有这些。或者我必须在映射和关系模式中使用cascade参数设置为“save update Or all”,以使3个服务在同一事务中协同工作(在这种情况下,将有3个保存方法annoted transactionnal)?您看到我试图理解的内容了吗?谢谢:)