我可以从Tomcat上下文设置JDBC隔离级别吗? [英] Can I set the JDBC isolation level from a Tomcat Context?

查看:128
本文介绍了我可以从Tomcat上下文设置JDBC隔离级别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Tomcat 6中运行的Web应用程序,并且已经设法将其配置为使用内置的DBCP连接池,并且一切运行良好,但是我怀疑它在错误的隔离级别上运行数据库.我希望它可以在未提交读状态下运行,但是我认为它可以在已提交读状态下运行,并且不知道如何设置它.

I have a web application running in Tomcat 6, and I've managed to configure it to use the built-in DBCP connection pooling, and all is working very well, however I suspect it is running in the wrong isolation level on the database. I'd like it to run in read uncommitted, but I think it's running in read committed and don't know how to set it.

这是我上下文的XML文件:

Here is my context's XML file:

<?xml version="1.0" encoding="UTF-8"?>
    <Context antiResourceLocking="false" privileged="true">
        <Resource 
            name="jdbc/Connection" 
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            driverClassName="net.sourceforge.jtds.jdbc.Driver"
            url="jdbc:jtds:sqlserver://...etc..."
        />
    </Context>

这是用于获得数据库连接的Java方法.

And this is the Java method used to get a database connection.

public Connection getDatabaseConnection() throws ServletException {
    try {
        InitialContext cxt = new InitialContext();
        if ( cxt == null ) {
            throw new ServletException( "ServletContext unavailable." );
        }

        DataSource ds = (DataSource)cxt.lookup( "java:/comp/env/jdbc/Connection" );
        if ( ds == null ) {
            throw new ServletException( "Data source not found!" );
        }

        Connection conn = ds.getConnection();
        return conn;
    } etc...

已经在getDatabaseConnection()中获得了连接,我意识到我可以使用conn.setIsolationLevel( Connection.TRANSACTION_READ_UNCOMMITTED )手动设置隔离级别,但这感觉不对,因为它涉及将隔离级别硬编码到Java中,或者对Java进行查找.每次需要新连接时的servlet上下文.

Having obtained the connection in getDatabaseConnection() I realise I could manually set the isolation level with conn.setIsolationLevel( Connection.TRANSACTION_READ_UNCOMMITTED ) but that feels wrong as it either involves hard-coding the isolation level into the Java, or performing a look-up to the servlet context every time a new connection is required.

我可以以某种方式在上下文XML中定义它,还是有我不知道的更好的方法?

Can I define this in the context XML somehow, or is there a better approach I'm not aware of?

推荐答案

是的,您可以在Resource元素中使用defaultTransactionIsolation属性进行设置.

Yes, you can set that with defaultTransactionIsolation attribute in the Resource element.

<Context antiResourceLocking="false" privileged="true">
        <Resource 
            defaultTransactionIsolation="READ_UNCOMMITTED"
            name="jdbc/Connection" 
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            driverClassName="net.sourceforge.jtds.jdbc.Driver"
            url="jdbc:jtds:sqlserver://...etc..."
        />

从文档中

defaultTransactionIsolation ¨

TransactionIsolation状态 此池创建的连接.一 以下内容之一:(请参阅javadoc)

TransactionIsolation state of connections created by this pool. One of the following: (see javadoc )

  • READ_COMMITTED
  • READ_UNCOMMITTED
  • REPEATABLE_READ
  • 可序列化
    • NONE
    • READ_COMMITTED
    • READ_UNCOMMITTED
    • REPEATABLE_READ
    • SERIALIZABLE
    • 这篇关于我可以从Tomcat上下文设置JDBC隔离级别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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