【问题标题】:How to use CachedRowSet in Google App Engine?如何在 Google App Engine 中使用 CachedRowSet?
【发布时间】:2015-06-09 02:48:09
【问题描述】:

我正在使用 Google App Engine(端点)生成一个简单的 API 服务。

我目前正在尝试更快、更高效地查询数据库,因此我决定仔细检查调用 ResultSet 和 Connections 的位置并关闭它们。

但是我想使用 CachedRowSet 并且几乎不可能,因为当我尝试访问 CachedRowSetImpl() 时,记录器会返回:

java.lang.NoClassDefFoundError: com.sun.rowset.CachedRowSetImpl 是一个 限制类。请参阅 Google App Engine 开发者指南 了解更多

所以我做了一些调查,Google App Engine's JRE whitelist 包括:

javax.sql.rowset.CachedRowSet

但不是

com.sun.rowset.CachedRowSetImpl

这有意义吗?否则我该如何初始化/使用CachedRowSet

我的代码:

public ResultSet getRecordsWhereInt(String table, String where, int condition) throws SQLException {

   c = DatabaseConnect();

   preStatement = c.prepareStatement("SELECT * FROM " + table + " WHERE " + where + " = ?");

   preStatement.setInt(1, condition);

   CachedRowSet rowset = new CachedRowSetImpl();

   rowset.populate(preStatement.executeQuery());

   c.close();

   return rowset;

}

任何帮助/指导将不胜感激。

更新 1 (09/06/2015):
进一步调查,我发现你可以使用RowSetProvider.newFactory().createCachedRowSet();实现CachedRowSetImpl的功能

但是,Google 限制使用 RowSetProvider()

这给我留下了唯一一个列入白名单的库RowSetFactory。于是我自己做了一个,按照Oracle's implementation实现RowSetFactory:

public class CachedRowSetFactory implements RowSetFactory {


    public CachedRowSet createCachedRowSet() throws SQLException {
        return new CachedRowSetImpl();
    }

    public FilteredRowSet createFilteredRowSet() throws SQLException {
        return null;
    }

    public JdbcRowSet createJdbcRowSet() throws SQLException {
        return null;
    }

    public JoinRowSet createJoinRowSet() throws SQLException {
        return null;
    }

    public WebRowSet createWebRowSet() throws SQLException {
        return null;
    }
}

然后像这样使用它:

CachedRowSet crs = new CachedRowSetFactory().createCachedRowSet();

但是,即使这样也失败了。上面的示例在我尝试使用工作结果集填充它时返回空指针异常。我猜工厂实现不是 100% 正确,或者我跳过了一些明显的东西。

更新 2(2015 年 11 月 6 日):
我尝试通过手动复制一些库并消除 App Engines 白名单中未涵盖的库来滚动我自己的 CachedRowSetImpl 和 RowSetProvider 实现。没有成功。我知道我需要放弃这一点,但我决心完成这项工作。

【问题讨论】:

    标签: java google-app-engine jdbc resultset google-cloud-endpoints


    【解决方案1】:

    CahecRowSet 是一个接口,因此显然它已被列入白名单。

    Interface CachedRowSet

    我猜您正在连接一个 Google CloudSQL 实例。

    来自 javadoc:

    CachedRowSet 对象是缓存其数据行的容器 内存中的行,这使得可以在不总是运行的情况下进行操作 连接到它的数据源。

    您正在尝试做的事情没有意义 IMO。 AppEngine 不会在不同请求之间保持状态,当您的应用程序自动扩展和新实例启动时当然也不会。

    为什么不将结果缓存在 Memcache 中,这将在后续请求和不同实例中持续存在。

    【讨论】:

    • 感谢您的回复。我对使用 CachedRowSet 的兴趣主要在于我在上面实现的方法getRecordsWhereInt。如果我返回结果集,而不是缓存的行集,如何正确关闭该结果集?我已经在查询中使用了 memcache。感谢您的帮助。
    • 您必须在 DTO 中复制结果集的元素并关闭结果集和连接,然后才能返回 HTTP 响应。使 DTO 可序列化,以便您可以将它们存储在 Memcache 中。归根结底,这是在缓存一行...
    猜你喜欢
    • 2010-11-20
    • 2017-12-04
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多