简体   繁体   English

如何在 Google App Engine 中使用 CachedRowSet?

[英]How to use CachedRowSet in Google App Engine?

I am using Google App Engine (Endpoints) to generate a simple API service.我正在使用 Google App Engine (Endpoints) 来生成一个简单的 API 服务。

I'm currently trying to make my queries to the database faster and more efficient, so I decided to double check where I was calling ResultSet and Connections and closing them.我目前正在尝试使我对数据库的查询更快更有效,因此我决定仔细检查我在哪里调用 ResultSet 和 Connections 并关闭它们。

However I want to use CachedRowSet and it almost seems impossible given that when I try to access CachedRowSetImpl() the logger returns:但是,我想使用CachedRowSet并且几乎不可能,因为当我尝试访问CachedRowSetImpl() ,记录器返回:

java.lang.NoClassDefFoundError: com.sun.rowset.CachedRowSetImpl is a restricted class. java.lang.NoClassDefFoundError: com.sun.rowset.CachedRowSetImpl 是一个受限类。 Please see the Google App Engine developer's guide for more details有关详细信息,请参阅 Google App Engine 开发人员指南

So I did some investigation and Google App Engine's JRE whitelist includes:所以我做了一些调查, Google App Engine 的 JRE 白名单包括:

javax.sql.rowset.CachedRowSet

but not但不是

com.sun.rowset.CachedRowSetImpl

Does that make any sense?这有任何意义吗? How can I initialize/use CachedRowSet otherwise?否则如何初始化/使用CachedRowSet

My code:我的代码:

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;

}

Any help would/guidance would be appreciated.任何帮助/指导将不胜感激。

Update 1 (09/06/2015):更新 1 (09/06/2015):
Investigating further, I found that you can implement CachedRowSetImpl's functionality using RowSetProvider.newFactory().createCachedRowSet();进一步调查,我发现您可以使用RowSetProvider.newFactory().createCachedRowSet();实现 CachedRowSetImpl 的功能RowSetProvider.newFactory().createCachedRowSet();

However, Google has also restricted use to RowSetProvider() .但是,Google限制了对RowSetProvider()

That left me with the only other whitelisted library RowSetFactory .这给我留下了唯一的其他白名单库RowSetFactory So I made my own, implementing RowSetFactory according to Oracle's implementation :所以我自己做了,根据Oracle 的实现来实现 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;
    }
}

And then used it like this:然后像这样使用它:

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

However, even this fails.然而,即使这样也失败了。 The above example returns a Null Pointer Exception when I try to populate it with a working result set.当我尝试用工作结果集填充它时,上面的示例返回空指针异常。 I'm guessing the factory implementation isn't 100% correct or that I'm skipping over something obvious.我猜工厂实现不是 100% 正确的,或者我跳过了一些明显的东西。

Update 2 (11/06/2015):更新 2 (11/06/2015):
I tried rolling my own implementation of CachedRowSetImpl, and of the RowSetProvider by duplicating some of the libraries by hand and eliminating the libraries not covered in App Engines Whitelist.我尝试通过手动复制一些库并消除 App Engines 白名单中未涵盖的库来滚动我自己的 CachedRowSetImpl 和 RowSetProvider 实现。 Was not successful.没有成功。 I know I need to give up on this but I'm determined to make this work.我知道我需要放弃这一点,但我决心完成这项工作。

CahecRowSet is an interface so obviously this is whitelisted. CahecRowSet 是一个接口,所以显然这是白名单。

Interface CachedRowSet 接口 CachedRowSet

I am guessing you are connecting a Google CloudSQL instance.我猜您正在连接 Google CloudSQL 实例。

from the javadoc :从 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. CachedRowSet 对象是数据行的容器,将其行缓存在内存中,这使得在不始终连接到其数据源的情况下进行操作成为可能。

What you are trying to do does not make sense IMO.您尝试做的事情对 IMO 没有意义。 AppEngine does not keep state between different requests and certainly not when your application auto-scales and new instances spin up. AppEngine 不会在不同请求之间保持状态,当然也不会在您的应用程序自动扩展和新实例启动时保持状态。

Why don't you cache your results in Memcache, this will persist over subsequent requests and different instances.为什么不将结果缓存在 Memcache 中,这将在后续请求和不同实例中持续存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM