0

我有一个使用单例数据库连接的 Java EE struts Web 应用程序。以前只有一个weblogic server,现在一个集群有两个weblogic server。

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

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

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;
}
4

1 回答 1

0

听起来您可能没有在使用连接结束时提交事务。

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

于 2012-07-05T10:19:05.387 回答