gpt4 book ai didi

java - 获取 SQLServerException 的数据访问对象类

转载 作者:行者123 更新时间:2023-12-01 09:58:21 25 4
gpt4 key购买 nike

执行sql语句时得到以下结果

SQLServerException: The server failed to resume the transaction. Desc:69d00000016.

我知道以下 DAO 实现是不正确的。我想知道以下代码的正确实现是什么,以及我的 connFactory 声明为静态的事实是否会导致上述错误。

private static DbConnectionFactory connFactory;

protected myDAO() {
myDAO.connFactory = DbConnectionFactoryHome.getHome().lookupFactory("facName");
}

public myReturn myAccessMethod(final int cod) throws BaseException {
Connection conn = null;
CallableStatement stmt = null;
ResultSet resSet = null;
myReturn ret= null;

try {
conn = myDAO.connFactory.getConnection();
stmt = conn.prepareCall("{call name (2)}");
stmt.setInt(1, cod);
resSet = stmt.executeQuery();
if (resSet.next()) {
ret = new myReturn(resSet.getInt("someValue"));
}
}
catch (SQLException sqle) {
throw new myException(sqle.getMessage(), (Throwable)sqle);
}
finally {
try {
if (resSet != null) {
resSet.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
return ret;
}

我应该从 connFactory 中删除静态修饰符或实现单例,以便再次调用构造函数时不会重新创建工厂吗?

最佳答案

我会让你的 DBConnectionFactory 成为单例。可以在这里找到如何执行此操作的一个很好的示例:Singleton DB Connectionfactory.

但是,我不确定您的问题是数据库连接工厂是静态的。它实际上可能与您使用结果集提取结果的方式有关。确保处理所有结果。 您应该包含更完整的堆栈跟踪。您可能想了解一下为什么会收到:“服务器无法恢复事务。”这里有一篇文章介绍了导致此错误的原因以及如何修复它:Failed to resume transaction

尝试做这样的事情:

CallableStatement stmt = connection.prepareCall("{call name (2)}");
stmt.setInt(1, cod);

stmt.execute();

ResultSet rs = (ResultSet)stmt.getObject(index);

//Loop results
while (rs.next()) {
ret = new myReturn(resSet.getInt("someValue")
}

关于java - 获取 SQLServerException 的数据访问对象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37008852/

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