java - 如何在 Google App Engine 中使用 CachedRowSet?

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

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

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

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

java.lang.NoClassDefFoundError: com.sun.rowset.CachedRowSetImpl is a restricted class. Please see the Google App Engine developer's guide for more details

所以我做了一些调查和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 (11/06/2015):
我尝试通过手动复制一些库并删除 App Engines 白名单中未涵盖的库来滚动我自己的 CachedRowSetImpl 和 RowSetProvider 实现。没有成功。我知道我需要放弃,但我决心完成这项工作。

最佳答案

CahecRowSet 是一个接口(interface),所以很明显这是白名单。

Interface CachedRowSet

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

来自 javadoc :

A CachedRowSet object is a container for rows of data that caches its rows in memory, which makes it possible to operate without always being connected to its data source.

您尝试做的事情在我看来毫无意义。 AppEngine 不会在不同的请求之间保持状态,当然在您的应用程序自动缩放和新实例启动时也不会。

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

关于java - 如何在 Google App Engine 中使用 CachedRowSet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722221/

相关文章:

java - VoltDB - 小数值有太多零

Java Spring rest 返回未经授权的 json

java - 如何让 Launch4j XML 配置正确捆绑 JRE?

python - 高复制数据存储中的重复条目

python - Django 应用引擎 : AttributeError: 'AnonymousUser' object has no attribute 'backend'

java - ORA-01461/BLOB/hibernate

java - OKHTTP:如何测量请求和响应中的总字节数?

python - 写入云存储时 GAE 内存泄漏,我可以流式传输吗?

java - addBatch() 和 executeBatch() 是线程安全的吗?

java - 如何使用 JDBC 和 PostgreSQL 获取大数据