我目前正在用java开发一个应用程序,使用Hibernate作为持久性管理器,JPA作为持久性管理hibernate的抽象.
我想知道在事务中包装结果查询的影响.我知道实体经理必须保持开放,因为懒惰地获取了所有这些事务中的事务错误?
这是一个具有事务激活/停用功能的代码示例.
public List<Exportdata> get(Integer max, EntityManager em, Boolean withTransaction) {
EntityTransaction tx = null;
try {
if (withTransaction) {
tx = em.getTransaction();
tx.begin();
}
Query query = em.createQuery("from Exportdata");
query.setMaxResults(10);
List<Exportdata> list = query.getResultList();
if (withTransaction)
tx.commit();
return list;
} catch (RuntimeException re) {
if (withTransaction)
if (tx != null && tx.isActive())
tx.rollback();
throw re;
}
}
Run Code Online (Sandbox Code Playgroud)
调用此函数时启用或禁用withTransaction有什么区别?
谢谢大家,弗雷德
我使用连接到MS-Access数据库的VB.net(200%)开发了一个应用程序,我使用TableAdapter和Dataset连接到Access DB文件.
我需要在保存到DB时实现一个简单的事务方法(commit,rollback)吗?
有没有办法在不需要使用内联SQL语句的情况下执行此操作?
谢谢,
据我所知,MySQL 中的 InnoDB 表,所有事务都用 START TRANSACTION 包装;并以 COMMIT 结束;除非明确说明不这样做。
如果我要明确定义交易块,这样做是否正确:
SET AUTOCOMMIT = 0;
START TRANSACTION;
[SQL STATEMENTS]
COMMIT;
SET AUTOCOMMIT = 1;
Run Code Online (Sandbox Code Playgroud)
此后的下一个事务会回到 InnoDb 默认处理事务的方式吗?我的目的是有时在我的应用程序中明确定义事务,但所有其他事务将由引擎处理。
我对 EJB 和 Bean 管理事务都很陌生。在放弃互联网之后,我发现我可以编写一个 EJ 会话 Bean,它以“bean 管理的方式”执行事务,如下所示:
@TransactionManagement(value=TransactionManagementType.BEAN)
@Stateless
public class OperationBean {
@Resource
private UserTransaction userTransaction;
public void operation() {
try{
userTransaction.begin();
Op1();
Op2();
Op3();
userTransaction.commit();
} catch(Exception e){
userTransaction.rollback();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不明白如果 Op3() 抛出异常会发生什么。Op1() 和 Op2() 如何撤消?
另一个问题是如何将其绑定到 JSF?我需要在 JSF 中编写一个表单,将其数据发送到服务器,OperationBean 根据接收到的数据执行 3 个操作。这些操作确实涉及数据库操作(3个更新),单独来看没有意义。
谢谢!
我在 SQL Server 2012 Express 中编写了这个存储过程。
ALTER PROCEDURE [Dictionaries].[InsertCountry]
(@p_countryName nvarchar(128)
, @ret_countryId int OUTPUT)
AS
BEGIN
DECLARE @TransactionName VARCHAR(20) = 'INST_COUNTRY';
BEGIN TRANSACTION @TransactionName;
IF len(@p_countryName) <= 3
BEGIN
SET @ret_countryId = null
ROLLBACK TRANSACTION @TransactionName;
END
INSERT INTO [Dictionaries].[CountryDetails] (name)
VALUES (@p_countryName);
SET @ret_countryId = @@IDENTITY
INSERT INTO [Dictionaries].[Places] (name, Discriminator , CountryDetails_Id, RegionDetails_Id, CityDetails_Id)
VALUES (@p_countryName, 'Country' , @ret_countryId, null, null);
COMMIT TRANSACTION @TransactionName;
END
Run Code Online (Sandbox Code Playgroud)
我以这种方式使用这个方法,但我收到消息
COMMIT TRANSACTION 请求没有对应的 BEGIN TRANSACTION。
代码:
declare @ret int = null …Run Code Online (Sandbox Code Playgroud) 我无法修改客户端上的 machine.config
嵌套事务中有没有其他方法可以让它们持续超过 10 分钟?隔离级别或任何其他选项是否有助于解决超时问题。我的内部事务DoWork()很好,我认为是外部事务超时了。
private void DoAllWork()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TransactionManager.MaximumTimeout))
{
DoWork();
scope.Complete();
}
}
private void DoWork()
{
Foreach(Some long running loop)
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TransactionManager.MaximumTimeout))
{
// Entity Framework queries / modification / Save Changes
scope.Complete();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的代码,由 Service -> RequestProcessor -> DAO 组成,每层有 2-3 个类(接口、抽象、具体)。
服务层:-
public interface Service {
public void saveOrUpdate(Object entity, String operationName);
}
}
public abstract class AbstractService implements Service{
public abstract ReqProcessor getRP();
@Override
public void saveOrUpdate(Object entity, String operationName) {
ReqProcessor hiberTestRP = getRP();
hiberTestRP.saveOrUpdate(entity, operationName);
}
}
@Component
public class ServiceImpl extends AbstractService {
@Autowired
public ReqProcessor hibertestRPImpl;
@Override
public HiberTestRP getRP() {
return hibertestRPImpl;
}
}
Run Code Online (Sandbox Code Playgroud)
ReqProcessor 层:-
public interface ReqProcessor {
public void saveOrUpdate(Object entity, String operationName); …Run Code Online (Sandbox Code Playgroud) spring hibernate transactions transactional spring-transactions
我知道 PostgreSQL 使用 MVCC 来处理并行事务,但我不确定在以下场景中会发生什么:
Client1 Client2 Time
-------------------------------------------------------------------
BEGIN BEGIN time=t0
Update row where id=1 Update row where id=1 time=t1
COMMIT COMMIT time=t2
Run Code Online (Sandbox Code Playgroud)
两个客户端都将在时间=t0 时启动事务,在时间=t1 时更新各自快照中 id=1 的同一行,并在时间=t2 时提交事务。由于提交是在同一时刻(时间=t2)完成的,它们是并行执行还是由 PostgreSQL 顺序执行?如果它们并行运行,是否有数据损坏的可能?谢谢。
编辑:“COMMIT”与 libPQ 一起使用。在 psql 中,COMMIT = END。
此方法以事务开头
@Transactional(propagation = Propagation.REQUIRED)
public Account generateDirectCustomerAccountEntity(AccountReq source, Map<String, String> headers, String workflowId) {
Account dbAccount = dCustomerModelToEntityMapper.map(source, headers);
dbAccount = saveAccount(source, dbAccount, headers, workflowId);
return dbAccount;
}
Run Code Online (Sandbox Code Playgroud)
这是一个映射器类,我在其中创建 DB Account 实体并映射 address 和 contacts 。
@Override
@LogExecutionTime(log=true)
public Account map(AccountReq accountReq, Map<String, String> headers) {
logger.info("AccountModelToEntityMapper.mapDirectCustomer*****START");
Account dbAccount = new Account();
AccountCode accountCode = dbService.getAccountCodeForDirectCustomer(accountReq);
String accountId = dbService.genarateAccountId(accountCode);
logger.info("genarated AccountID for Direct Customer is={}", accountId);
dbAccount.setAccountId(accountId);
setAccountContact(dbAccount, accountReq, headers);
setAddress(dbAccount, accountReq);
dbAccount.setAccountType(accountCode.getCodeId());
return dbAccount;
}
Run Code Online (Sandbox Code Playgroud)
当我不在地图内调用下面的方法时,一切正常。但是当我调用它时,我得到了标题中描述的错误。
private void …Run Code Online (Sandbox Code Playgroud)