DefaultPreparedStatement 不可序列化

DefaultPreparedStatement not serializable

我正在使用 Datastax 驱动程序和 Cassandra 3.10 构建一个网络应用程序,当我部署到 Tomcat 并启动它时,我收到一条错误消息:

Caused by: java.io.NotSerializableException: com.datastax.driver.core.DefaultPreparedStatement

尽管我没有在代码中的任何地方使用它,但我所有的准备语句都是以下形式:

private transient PreparedStatement     selectAssetClassStmt;
selectAllAssetClassesStmt = cassandraDatasource.getSession().prepare("select code,description from  asset_class where code = ?");

如果你使用普通的 Cassandra(没有 Spring 数据),你应该使用 com.datastax.driver.mapping.annotations.@Transient annotation :

Whenever this annotation is added on a field, the field will not be mapped to any column (neither during reads nor writes).

import com.datastax.driver.mapping.annotations.Transient;
...
@Transient
private PreparedStatement selectAssetClassStmt;

不是直接的问题,但为什么要声明一个 PreparedStatement 字段?
它不应该是必需的,它很容易出错,并且可能会导致意外行为,因为您不知道它在以后使用时的状态。

PreparedStatement 变量应该只是一个局部变量,其生命周期是一种访问数据库的方法。