如何在JPA中自动提交SQL Server事务? [英] How do you auto-commit an SQL Server transaction in JPA?

查看:986
本文介绍了如何在JPA中自动提交SQL Server事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我需要使用事务来持久化实体?我可以添加到persistence.xml中进行自动提交吗?

Why do I need to use a transaction to persist an Entity? Is there something I can add to my persistence.xml to auto-commit?

这不会插入:

em.persist(this);

但是确实如此:

em.getTransaction().begin();
em.persist(this);
em.getTransaction().commit();

我想我最初的参考点是此GWT文件

I guess my initial reference point was this GWT doco

public void persist() 
{
   EntityManager em = entityManager();
   try 
   {
     em.persist(this);
   } 
   finally 
   {
     em.close();    
   }  
}

推荐答案

JPA并未定义此类行为;根据规范,将任何"persist()"定义为等待直到下一个事务.显然,JPA的某些实现(例如 DataNucleus JPA )确实提供了(自动提交)功能,但这超出了JPA规范(并非该规范的一部分)

JPA does not define such behaviour; any "persist()" is defined to wait til the next transaction according to the spec. Obviously some implementations of JPA (such as DataNucleus JPA) do provide that (auto-commit) facility, but it's beyond (not part of) the JPA spec

这篇关于如何在JPA中自动提交SQL Server事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆