我在我的演示应用程序中使用Hibernate + Spring + Jsf我希望Spring将handel事务管理所以我在application-context.xml文件中创建了这些条目..
<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/beans
http://www.springframework.org/schema/beans/spring-beans-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">
<!-- Beans Declaration -->
<bean id="User" class="com.otv.model.User"/>
<!-- User Service Declaration -->
<bean id="UserService" class="com.otv.user.service.UserService">
<property name="userDAO" ref="UserDAO" />
</bean>
<!-- User DAO Declaration -->
<bean id="UserDAO" class="com.otv.user.dao.UserDAO">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="user" value="postgres" />
<property name="password" value="hariom" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.otv.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- Transaction Manager is defined -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
</beans>
和UserService.java类
package com.otv.user.service;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.otv.model.User;
import com.otv.user.dao.IUserDAO;
/**
*
* User Service
*
* @author onlinetechvision.com
* @since 25 Mar 2012
* @version 1.0.0
*
*/
@Transactional(readOnly = true)
public class UserService implements IUserService {
// UserDAO is injected...
IUserDAO userDAO;
/**
* Add User
*
* @param User user
*/
@Transactional(readOnly = false)
public void addUser(User user) {
getUserDAO().addUser(user);
}
/**
* Delete User
*
* @param User user
*/
@Transactional(readOnly = false)
public void deleteUser(User user) {
getUserDAO().deleteUser(user);
}
/**
* Update User
*
* @param User user
*/
@Transactional(readOnly = false)
public void updateUser(User user) {
getUserDAO().updateUser(user);
}
/**
* Get User
*
* @param int User Id
*/
public User getUserById(int id) {
return getUserDAO().getUserById(id);
}
/**
* Get User List
*
*/
public List<User> getUsers() {
return getUserDAO().getUsers();
}
/**
* Get User DAO
*
* @return IUserDAO - User DAO
*/
public IUserDAO getUserDAO() {
return userDAO;
}
/**
* Set User DAO
*
* @param IUserDAO - User DAO
*/
public void setUserDAO(IUserDAO userDAO) {
this.userDAO = userDAO;
}
}
现在我正在做我的豆子
@ManagedProperty(value="#{UserService}")
IUserService userService;
*/
public IUserService getUserService() {
return userService;
}
/**
* Set User Service
*
* @param IUserService - User Service
*/
public void setUserService(IUserService userService) {
this.userService = userService;
}
public String addUser() {
try {
User user = new User();
user.setId(getId());
user.setName(getName());
user.setSurname(getSurname());
userService = getUserService();
userService.addUser(user);
userService.deleteUser(null);
return SUCCESS;
} catch (DataAccessException e) {
e.printStackTrace();
}
return ERROR;
}
此代码中userService.deleteUser(null);
我有力地通过异常,但仍保存在数据库中的数据?为什么Trasaction管理会处理这种情况,如果异常为什么不回滚数据呢?
UserDAO Class
package com.otv.user.dao;
import java.util.List;
import com.otv.model.User;
import org.hibernate.SessionFactory;
public class UserDAO implements IUserDAO {
private SessionFactory sessionFactory;
/**
* Get Hibernate Session Factory
*
* @return SessionFactory - Hibernate Session Factory
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* Set Hibernate Session Factory
*
* @param SessionFactory - Hibernate Session Factory
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* Add User
*
* @param User user
*/
public void addUser(User user) {
getSessionFactory().getCurrentSession().save(user);
}
/**
* Delete User
*
* @param User user
*/
public void deleteUser(User user) {
getSessionFactory().getCurrentSession().delete(user);
}
/**
* Update User
*
* @param User user
*/
public void updateUser(User user) {
getSessionFactory().getCurrentSession().update(user);
}
/**
* Get User
*
* @param int User Id
* @return User
*/
public User getUserById(int id) {
List list = getSessionFactory().getCurrentSession()
.createQuery("from User where id=?")
.setParameter(0, id).list();
return (User)list.get(0);
}
/**
* Get User List
*
* @return List - User list
*/
public List<User> getUsers() {
List list = getSessionFactory().getCurrentSession().createQuery("from User").list();
return list;
}
}
答案 0 :(得分:2)
似乎事务边界是UserService类的每个方法。当你说userService.addUser(user)
一个交易开始并完成时。因此,当你说userService.deleteUser(null)
它是另一个事务时 - 尽管它失败了,但是之前的事务没有回滚,因为它已经被评估了。
如果您想在一个单独的事务中执行所有操作,请使用@Transactional注释addUser()(或在程序化事务中包含)
答案 1 :(得分:1)
方法上的@Transactional
注释定义了事务的边界(除非其他人已经运行了“外部事务”)。同样,类的@Transactional
注释使所有单个调用该类事务的实例的(公共)方法,但不 make 连续的方法调用在单个事务中包装的同一实例。相反,每个方法调用都在自己的事务中运行,如果方法正常返回,则提交事务。 (默认情况下,如果抛出已检查的异常,它甚至会被提交,尽管您可以使用rollbackFor
和noRollbackFor
参数更改此内容。)
所以在你的情况下,你有两个交易;这个电话的一个:
userService.addUser(user);
成功创建用户并在完成对addUser
的调用时提交,第二个是
userService.deleteUser(null);
当NullPointerException引发方法调用时回滚。 (我猜你在这里得到一个NPE,因为这是RuntimeExcepotion的子类,它会导致回滚。)
如果您希望在单个事务中同时拥有这两个语句,则需要将事务置于更高级别。这里的'@ Transactional`注释不会受到伤害,但它们并不是你想要的。