我正在使用 Datastax 驱动程序和 Cassandra 3.10 构建一个 Web 应用程序,当我部署到 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 Data),则应该使用 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
变量应该只是一个局部变量,其生命周期是访问数据库的方法。
关于java - 默认PreparedStatement不可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45397300/