[英]Spring Hibernate transaction exception rollback
Update 12/12/2011 更新12/12/2011
The application is making use of a Generic DAO class that can be found here . 该应用程序利用可以在此处找到的Generic DAO类。
Based on re-reading the spring manual on the usage of @Repository it would appear that I need to add this to my GenericDAO implementation in order for spring to handle the exception translations. 在重新阅读有关@Repository用法的spring手册的基础上,我似乎需要将其添加到我的GenericDAO实现中,以便spring处理异常翻译。
I added the @Repository to the GenericDAO but, results did not change. 我将@Repository添加到GenericDAO,但是结果没有改变。 The parent object is still being persisted into the database.
父对象仍保留在数据库中。
Using Spring 2.5.6, JBoss 5.1 with its hibernate version, Spring MVC, MySQL 使用Spring 2.5.6,JBoss 5.1及其休眠版本,Spring MVC,MySQL
I am doing some testing to make sure that my transactions will get rolled back correctly in the event of an exception. 我正在做一些测试,以确保在发生异常的情况下我的交易能够正确回滚。 In the below code the second method: createProfile will throw a NullPointerException which prevents the method from completing.
在下面的代码中,第二个方法:createProfile将抛出NullPointerException,它阻止该方法完成。 I had expected that the transaction would of been rolled back.
我曾预计该交易将被回滚。 What I am finding is that user object is persisted into the database.
我发现的是该用户对象被持久保存到数据库中。 What I want is that if the second operation fails then the first operation needs to roll back as well.
我想要的是,如果第二个操作失败,那么第一个操作也需要回滚。
I have done some changes in which I changed the propagation to REQUIRED_NEW for the parent method but did not see any changes in the behavior. 我进行了一些更改,其中将父方法的传播更改为REQUIRED_NEW,但未看到行为的任何更改。
I have turned on logging for spring transaction. 我已开启春季交易的日志记录。
I have been looking over different posting here and reviewing what spring has to say on transactions as well . 我一直在这里查看不同的发布,并且还回顾了春季在交易中要说的内容 。 I seem to be missing something along the lines of transaction behavior.
我似乎在某种程度上缺少交易行为。
What am I doing incorrect here. 我在这里做错了什么。
I have the following defined 我有以下定义
@Transactional(propagation = Propagation.SUPPORTS)
public class ProfileServiceImpl implements ProfileService
{
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public boolean createProfile(UserDTO pUserDTO, ContactInfoDTO pContactInfoDTO)
{
boolean retVal = false;
if(this.createProfile(pUserDTO))
{
if(this.addAddressToUser(pContactInfoDTO, pUserDTO.getId()))
{
retVal = true;
}
}
return retVal;
}
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public boolean createProfile(UserDTO userDTO)
{
boolean retVal = false;
if(this.getProfileQueries().isLoginUnique(userDTO.getLogin(),userDTO.getSiteInfoId()))
{
User user = new User();
BeanUtils.copyProperties(userDTO, user);
user.setPassword(this.stringDigester.digest(userDTO.getPassword()));
user.setSiteInfo(this.getSiteInfoDao().read(userDTO.getSiteInfoId()));
user.setSecurityLevel(SecurityLevel.ROLE_USER);
user.setStatus(Status.ACTIVE);
Long pk = this.getUserDao().create(user);
userDTO.setId(pk);
retVal = true;
}
return retVal;
}
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public boolean addAddressToUser(ContactInfoDTO pAddress, Long pProfileId)
{
boolean retVal = false;
if(null != pAddress && null != pProfileId)
{
ContactInfo contactInfo = new ContactInfo();
BeanUtils.copyProperties(pAddress, contactInfo);
User user = this.getUserDao().read(pProfileId);
contactInfo.setUser(user);
this.getContactInfoDao().create(contactInfo);
user.getUserAddress().setProfileAddress(contactInfo);
user.getUserAddress().setBillingAddress(contactInfo);
this.getUserDao().update(user);
retVal = true;
}
return retVal;
}
}
Data config 数据配置
<!-- TRANSACTION MANAGER-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- ANNOTATION DRIVEN TRANSACTIONS -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/MySqlDS"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="jndiDataSource"/>
<property name="annotatedClasses">
<list>
<value>com.vsg.dataaccess.user.entity.User</value>
<value>com.vsg.dataaccess.user.entity.ContactInfo</value>
<value>com.vsg.dataaccess.user.entity.UserAddress</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="org.hibernate.envers.audit_table_suffix">_aud</prop>
<prop key="org.hibernate.envers.revision_field_name">rev_number</prop>
<prop key="org.hibernate.envers.revision_type_field_name">rev_type</prop>
<prop key="org.hibernate.envers.revision_on_collection_change">true</prop>
</props>
</property>
<property name="entityInterceptor">
<bean class="vsg.ecotrak.dataaccess.framework.hibernate.interceptor.AuditTrailInterceptor"/>
</property>
</bean>
确保(在IDE的调试模式下)Spring的AOP代理包装了ProfileServiceImpl类的实例(这意味着Spring事务已正确配置),还请提供事务管理器和数据源的* .xml配置(等)。
I think the autocommit is causing the issue. 我认为自动提交是造成此问题的原因。 You would want to turn his off and manually manage the transactions if you want a rollback on first query when second fails .
如果要在第二个查询失败时回滚第一个查询,则需要关闭其功能并手动管理事务。 Else the first query will be autocommited.
否则,第一个查询将被自动提交。 Think about this .
想想看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.