gpt4 book ai didi

sql-server - 使用 CFTRANSACTION 和 ORM EntityLoad 设置隔离级别

转载 作者:行者123 更新时间:2023-12-03 09:33:28 26 4
gpt4 key购买 nike

我正在使用 ColdFusion 9 和 MS SQL Server。

我正在尝试使用:

<cftransaction isolation="read_uncommitted">
<cfset X = EntityLoad('table',row_id,true)>
</cftransaction>

更改某些事务中的隔离级别

但是从数据源查看日志我得到了这个:
09:20:32.688)>> Connection[6].prepareStatement(String sql)
09:20:32.688)>> sql = select ..... _ID=?
09:20:32.688)>> OK (PreparedStatement[871])
09:20:32.689)>> PreparedStatement[871].executeQuery()
09:20:33.594)>> OK (ResultSet[989])
09:20:33.606)>> Connection[6].setTransactionIsolation(int level)
09:20:33.606)>> level = 1
09:20:33.962)>> OK
09:20:34.141)>> Connection[6].setTransactionIsolation(int level)
09:20:34.141)>> level = 2
09:20:34.501)>> OK

如果将 EntityLoad 替换为简单的 cfquery 调用,我会得到:
09:24:30.164)>> Connection[6].setTransactionIsolation(int level)
09:24:30.164)>> level = 1
09:24:30.519)>> OK
09:24:30.519)>> Statement[37].execute(String sql, int autoGeneratedKeys)
09:24:30.519)>> sql = select ...
09:24:30.519)>> autoGeneratedKeys = 1
09:24:30.699)>> OK (true)
09:24:30.879)>> Connection[6].setTransactionIsolation(int level)
09:24:30.879)>> level = 2
09:24:31.234)>> OK

因此,当我使用 ORM 的 EntityLoad 时,似乎隔离级别设置不正确。

有谁知道为什么?使用 ORM 设置隔离级别的更好方法是什么?

最佳答案

我在 Curt Gratz 的 ColdBox 博客上找到了一篇关于此的帖子:Coldfusion ORM - Transaction Isolation Level...完整阅读那篇文章,它有一些优点。但基本上 Curt 发现你可以创建一个自定义的休眠配置文件来指定各种设置。

来自那篇博文:

So, since changing the isolation level isn't one of the settings available in the ormsettings, I had to create a custom hibernate config file.

At first I thought this would be difficult as I thought that I would have to insert all the "standard" settings the Coldfusion implementation of hibernate was already using, but it turns out, you only have to set any additional properties you wish to use. Here is a list of available hibernate configuration options.

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

So, on to some code...

Here is my newly created hibernate config file.

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<!-- a SessionFactory instance listed as /jndi/name -->

<session-factory

name="java:hibernate/SessionFactory">

<!-- properties -->

<property name="connection.isolation">1</property>

</session-factory>

</hibernate-configuration>

Notice one thing in the config file. <property name="connection.isolation">1</property>. Why one you ask...

Here is the int values for each of the isolation levels available.

java.sql.Connection
public static final int TRANSACTION_NONE 0
public static final int TRANSACTION_READ_COMMITTED 2
public static final int TRANSACTION_READ_UNCOMMITTED 1
public static final int TRANSACTION_REPEATABLE_READ 4
public static final int TRANSACTION_SERIALIZABLE 8

Source: http://java.sun.com/j2se/1.5.0/docs/api/constant-values.html#java.sql.Connection.TRANSACTION_READ_COMMITTED

Then in my application.cfc I set

<cfset this.ormsettings = {dialect="MicrosoftSQLServer", ormconfig="hibernate.xml"}>  

Now, be sure that you have your hibernate.xml file somewhere outside of webroot to protect it from being read by any evil eyes.

There you go, hope this helps you as it took me a lot of digging to figure it out



此信息完全归功于 Curt Gratz。
重新发布在这里以防将来该页面被删除。

关于sql-server - 使用 CFTRANSACTION 和 ORM EntityLoad 设置隔离级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33234024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com