簡體   English   中英

TransactionException:事務未成功啟動

[英]TransactionException: Transaction not successfully started

我跟隨道:

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public void add(User user) {
        Session session = sessionFactory.getCurrentSession();
        session.save(user);
        session.getTransaction().commit();
    }
}

它是從

@Controller
public class HomeController {    

    @Autowired
    private UserDao userDao;


    @RequestMapping(value = "/test")
    public ModelAndView test() {
        User user = new User();
        user.setName("34r");

        userDao.add(user);

        ModelAndView model = new ModelAndView("home");
        model.addObject("userList", null);
        return model;

    }

}

在瀏覽器中我嘗試訪問此鏈接

最后我得到以下堆棧跟蹤:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/SpringMvcHibernateXML] threw exception [Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started] with root cause
org.hibernate.TransactionException: Transaction not successfully started
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:172)

我有以下配置:

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

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

如何解決這個問題?

你不應該做session.getTransaction().commit(); 這個,@ transnsaction將負責它。 刪除它,你應該沒事。

在哪里開始交易。 我看不到這行session.beginTrainsaction(); 一旦開始事務,那么只有您可以提交和回滾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM