我正在编写一个小型拍卖应用程序,我的出价被确定地记录下来是非常重要的.毕竟,拍卖的最后几秒钟对买家来说是关键时刻,我不能让他们同时竞拍和比赛.

当然,这就是事务隔离的用途.我可以将隔离级别设置为可序列化,我们就都设置好了.

但是其他的请求呢?如果人们正在查看配置文件或发送消息,这些请求不需要接近这种事务隔离.对于这些请求,读提交隔离级别是完全可以接受的.

我将事务级别设置为hibernate属性hibernate.connection.isolation的一部分,但我真的希望能够对每个请求执行session.setTransactionIsolation(newIsolation)这样的操作.

推荐答案

Session session = getSession(dataSource, sessionFactory, Connection.TRANSACTION_SERIALIZABLE);

public Session getSession(DataSource dataSource, SessionFactory sessionFactory, int isolationLevel){

  // Get connection from current dataSource and set new isolation
  Connection connectionWithNewIsolation = dataSource.getConnection();
  connectionWithNewIsolation.setTransactionIsolation(isolationLevel);

  // Get session from current sessionFactory with the new isolation
  Session session = sessionFactory.openSession(connectionWithNewIsolation);

  // Hibernate 4.3
  //SessionFactory.openStatelessSession(Connection connection)
  // Hibernate 3.6
  //SessionFactory.openSession(Connection connection)
  //SessionFactory.openStatelessSession(Connection connection)

  return session;
}

Database相关问答推荐

即使将enable_seqscan设置为关闭,也未使用数组列上的 GIN 索引?

在 model.save() 中处理竞争条件

tzname字段/时区标识符名称的最大长度

Spring Boot:如何使用多个模式并在运行时动态 Select 使用哪一个

Java 数据库连接池(BoneCP vs DBPool vs c3p0)

苹果 ios 购买收据数据的可能最大长度是多少?

Oracle 数据库统计信息应该多久运行一次?

South migration error: NoMigrations exception for django.contrib.auth

是否有一个 postgres 命令来列出/删除所有materialized视图?

为什么用 varbinary 而不是 varchar

Python中准备好的语句和参数化查询之间的混淆

Android - ViewHolder 模式是否在 CursorAdapter 中自动实现?

数据库触发器的命名约定

在 MYSQL 的子查询中使用 LIMIT 关键字的替代方法

如何通过数据库链接执行 Oracle 存储过程

Spring data : CrudRepository 的保存方法和更新

是否有任何支持协议缓冲区的数据库?

我将如何为读写操作实现单独的数据库?

如何将空值传递给外键字段?

如何使用 liquibase,一个具体的例子