春季,休眠,事务管理,未提交



所以,我尝试使用Spring进行事务管理。最后我的应用程序已经工作了,但删除不提交。在使用 Spring 之前,我在 DAO 中管理事务,我认为在我可以删除此代码之后。但是现在删除不提交到DB。

我的道:

protected Session getSession() {
    Session session = sessionFactory.openSession();
    ThreadLocalSessionContext.bind(session);
    return session;
}
public void delete(T t) {
    Session session = getSession();
// **this approach I used before**
//  session.beginTransaction();
//  try {
//  session.delete(t);
//  session.getTransaction().commit();
//  } catch (Exception e) {
//      session.getTransaction().rollback();
//  }
    session.delete(t);
    }

我的应用程序上下文:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://ngs-java-srv.synapse.com:3306/mybase" />
        <property name="username" value="user" />
        <property name="password" value="password" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- <property name="configLocation" value="hibernate.cfg.xml" /> -->
        <property name="packagesToScan" value="todolist.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    <!-- dao beans -->
    <bean id="userDao"
        class="todolist.dao.hibernate.UserDaoImpl">
        <constructor-arg>
            <value>todolist.entity.User</value>
        </constructor-arg>
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="itemDao"
        class="todolist.dao.hibernate.ItemDaoImpl">
        <constructor-arg>
            <value>todolist.entity.Item</value>
        </constructor-arg>
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- service bean -->
    <bean id="userService" class="todolist.service.UserServiceImpl">
        <property name="userDao" ref="userDao" />
        <property name="itemDao" ref="itemDao" />
    </bean>
    <!-- transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
     <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
    <!-- <tx:annotation-driven/> -->
<!--    <aop:config>
        <aop:pointcut id="serviceMethods"
            expression="execution(* todolist.service.UserService.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRES_NEW" />
            <tx:method name="deleteItem" no-rollback-for="Exception" />
        </tx:attributes>
    </tx:advice> -->
    <!-- backbeans -->
    <bean id="userLogin" class="todolist.jsf.UserLogin"
        scope="request">
        <property name="userService" ref="userService" />
    </bean>
    <bean id="userLogged" class="todolist.jsf.UserLogged"
        scope="session">
        <aop:scoped-proxy />
    </bean>
    <bean id="userRegister" class="todolist.jsf.UserRegister"
        scope="request">
        <property name="userService" ref="userService" />
    </bean>
    <bean id="createItem" class="todolist.jsf.CreateItem"
        scope="request">
        <property name="userService" ref="userService" />
    </bean>
    <bean id="todoList" class="todolist.jsf.TodoList"
        scope="request">
        <property name="userService" ref="userService" />
    </bean>
</beans>

UserServiceImpl 类

package todolist.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import todolist.exception.AuthorizationError;
import todolist.exception.DuplicateLoginsException;
import todolist.service.StringToHashTool;
import todolist.dao.ItemDao;
import todolist.dao.UserDao;
import todolist.entity.Item;
import todolist.entity.User;
public class UserServiceImpl implements UserService {
    //private static final Logger log = Logger.getLogger(UserServiceImpl.class);
    private UserDao userDao;
    private ItemDao itemDao;
    public void setItemDao(ItemDao itemDao) {
    this.itemDao = itemDao;
    }
    public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean isUserExists(String login) {
    return (userDao.getUserByLogin(login) != null);
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean isPasswordCorrect(String login, String password) {
    if (isUserExists(login)) {
        return userDao.getUserByLogin(login).getPassword()
            .equals(StringToHashTool.getHash(password));
    } else {
        return false;
    }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public User login(String login, String password) {
    if (isPasswordCorrect(login, password)) {
        return userDao.getUserByLogin(login);
    } else {
        throw new AuthorizationError("Incorrect password");
    }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean register(String login, String password) {
    if (isUserExists(login)) {
        throw new DuplicateLoginsException("Login " + login + " is already used.");
    } else {
        User user = new User();
        user.setLogin(login);
        user.setPassword(StringToHashTool.getHash(password));
        userDao.save(user);
        return true;
    }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logout() {
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Item createItem(Long creator_id, Long performer_id,
        String description, Date dueDate) {
    Item item = new Item();
    User user = userDao.getById(creator_id);
    item.setCreator(user);
    user = userDao.getById(performer_id);
    item.setPerformer(user);
    item.setDescription(description);
    item.setStartDate(new Date());
    item.setDueDate(dueDate);
    itemDao.save(item);
    return item;
    }
    @Override    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void deleteItem(Long item_id) {
    Item item = itemDao.getById(item_id);
    itemDao.delete(item);
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public List<Item> getUserItems(String login) {
    User user = userDao.getUserByLogin(login);
    return itemDao.getItemsByPerformer(user.getId());
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public List<User> getUsers() {
    return userDao.getUsers();
    }
}
<aop:pointcut 
    id="serviceMethods"
    expression="execution(*todolist.service.UserService.*(..))" 
/>

第一步:

将其更改为:

<aop:pointcut 
    id="serviceMethods"
    expression="todolist.service.UserServiceImpl.delete()" 
/>

查看删除是否开始提交。我只是想确保你清除任何错别字和你有什么。如果此显式配置失败,则配置一定有问题。

我已经成功地使用了Spring HibernateTransactionManager对象和注释驱动的配置。 在我的应用程序上下文中,我只是声明以下两个 bean:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="session_factory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

然后在我的服务层中,您的UserServiceImpl例如:

public class UserServiceImpl implements UserService {
private final UserDao userDao;
private final ItemDao itemDao;
public UserServiceImpl(UserDao userDao, ItemDao itemDao) {
    this.userDao = userDao;
    this.itemDao = itemDao;
}
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public boolean isUserExists(String login) {
    return (userDao.getUserByLogin(login) != null);
}
...

事务注释尽可能靠近视图(在 MVC 中(非常重要。 原因是 1( 性能和 2( 原子事务。 如果调用 UserService 的调用在单个函数调用中调用 isUserExists、isPasswordCorrect 和登录方法,则您可能希望在单个事务中执行这些方法。 为此,请从 UserServiceImpl 中删除@Transactional注释,并将其移动到调用类。 这可确保单个事务用于所有三个方法调用。对于原子执行,这也有效。 想想经典的银行汇款示例。 如果在用户服务上有两个单独的事务,则前两个方法调用可以成功,最后一个方法调用可以失败。将提交第一个事务,因为它们位于单独的事务块中,这将使数据库处于不一致状态。当然,原子事务主要与写入数据的事务相关。

此外,不要将事务注释添加到接口中,而是将它们保留在实现中。

然后在我的DAO中,我只需做一个sessionFactory.getCurrentSession((并从那里开始。 getCurrentSession(( 是线程安全的,但对你的 DAO 保持私有。

在你的DAO中使用getCurrentSession而不是openSession,这样getSession方法看起来像这样:

protected Session getSession() {
    return sessionFactory.getCurrentSession();
}

您正在创建一个使用不同数据库连接的新会话,因此为服务设置的事务不适用。

此外,当您配置它们时,DAO 将具有单例的默认范围;将实体作为构造函数参数传递是没有意义的。单一实例不应具有特定于某些单个事务的实例状态。

最新更新