[英]How to use CachedRowSet in Google App Engine?
我正在使用 Google App Engine (Endpoints) 来生成一个简单的 API 服务。
我目前正在尝试使我对数据库的查询更快更有效,因此我决定仔细检查我在哪里调用 ResultSet 和 Connections 并关闭它们。
但是,我想使用CachedRowSet
并且几乎不可能,因为当我尝试访问CachedRowSetImpl()
,记录器返回:
java.lang.NoClassDefFoundError: com.sun.rowset.CachedRowSetImpl 是一个受限类。 有关详细信息,请参阅 Google App Engine 开发人员指南
所以我做了一些调查, Google App Engine 的 JRE 白名单包括:
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 的功能RowSetProvider.newFactory().createCachedRowSet();
但是,Google也限制了对RowSetProvider()
。
这给我留下了唯一的其他白名单库RowSetFactory
。 所以我自己做了,根据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;
}
}
然后像这样使用它:
CachedRowSet crs = new CachedRowSetFactory().createCachedRowSet();
然而,即使这样也失败了。 当我尝试用工作结果集填充它时,上面的示例返回空指针异常。 我猜工厂实现不是 100% 正确的,或者我跳过了一些明显的东西。
更新 2 (11/06/2015):
我尝试通过手动复制一些库并消除 App Engines 白名单中未涵盖的库来滚动我自己的 CachedRowSetImpl 和 RowSetProvider 实现。 没有成功。 我知道我需要放弃这一点,但我决心完成这项工作。
CahecRowSet 是一个接口,所以显然这是白名单。
我猜您正在连接 Google CloudSQL 实例。
从 javadoc :
CachedRowSet 对象是数据行的容器,将其行缓存在内存中,这使得在不始终连接到其数据源的情况下进行操作成为可能。
您尝试做的事情对 IMO 没有意义。 AppEngine 不会在不同请求之间保持状态,当然也不会在您的应用程序自动扩展和新实例启动时保持状态。
为什么不将结果缓存在 Memcache 中,这将在后续请求和不同实例中持续存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.