单例数据库连接是否会影响weblogic集群环境中的性能?

时间:2012-07-05 09:59:48

标签: database java-ee struts weblogic cluster-computing

我有一个使用单例数据库连接的Java EE struts Web应用程序。过去,只有一个weblogic服务器,但现在,集群中有两个weblogic服务器。

会话复制已经过测试,可以在此群集中运行。 Web应用程序包含一些链接,这些链接将打开不同的表单供用户填写。每个表单都有一个动态下拉列表,根据单击的表单填充一些值。这些下拉列表值是从oracle数据库中检索的。

一个独特的问题是,单击的第一个表单可能需要大约2-5秒,而单击的第二个表单可能需要永久加载或超过5分钟。我已经检查了代码并且碰巧知道问题在于尝试调用db连接的一个实例时。这可能是一个僵局吗?

public static synchronized DataSingleton getDataSingleton()
throws ApplicationException {
        if (myDataSingleton == null) {
            myDataSingleton = new DataSingleton();
        }
        return myDataSingleton;
    }

任何帮助解释这种情况都将不胜感激。

谢谢

A sample read operation calling Singleton 

String sql = "...";
DataSingleton myDataSingleton = DataSingleton.getDataSingleton();
conn = myDataSingleton.getConnection();

        try {

            PreparedStatement pstmt = conn.prepareStatement(sql);

            try {
                pstmt.setString(1, userId);
                ResultSet rs = pstmt.executeQuery();

                try {
                    while (rs.next()) {
                        String group = rs.getString("mygroup");
                    }

                } catch (SQLException rsEx) {
                    throw rsEx;

                } finally {
                    rs.close();
                }
            } catch (SQLException psEx) {
                throw psEx;

            } finally {
                pstmt.close();
            }
        } catch (SQLException connEx) {
            throw connEx;

        } finally {
            conn.close();
        }



 The Singleton class
 /**
 * Private Constructor looking up for Server's Datasource through JNDI
 */
private DataSingleton() throws ApplicationException {
    try {
        Context ctx = new InitialContext();
        SystemConstant mySystemConstant = SystemConstant
                .getSystemConstant();

        String fullJndiPath = mySystemConstant.getFullJndiPath();
        ds = (DataSource) ctx.lookup(fullJndiPath);

    } catch (NamingException ne) {
        throw new ApplicationException(ne);
    }
}

/**
 * Singleton: To obtain only 1 instance throughout the system
 * 
 * @return DataSingleton
 */
public static synchronized DataSingleton getDataSingleton()
        throws ApplicationException {
    if (myDataSingleton == null) {
        myDataSingleton = new DataSingleton();
    }

    return myDataSingleton;
}

/**
 * Fetching SQL Connection through Datasource
 * 
 */
public Connection getConnection() throws ApplicationException {
    Connection conn = null;
    try {

        if (ds == null) {
        }

        conn = ds.getConnection();

      } catch (SQLException sqlE) {
        throw new ApplicationException(sqlE);
    }
    return conn;
}

1 个答案:

答案 0 :(得分:0)

听起来您在使用连接时可能不会提交交易。

DataSingleton中有什么 - 它是数据库连接吗?允许多个线程访问同一个数据库连接是行不通的,例如,如果您有多个用户。为什么不使用数据库连接池,例如DataSource?

相关问题