Issue
I am building a webapp using Datastax driver and Cassandra 3.10, when I deploy to Tomcat and start it up I get an error stating that:
Caused by: java.io.NotSerializableException: com.datastax.driver.core.DefaultPreparedStatement
even though I do not use that anywhere in the code, all of my Prepared Statements are of the form:
private transient PreparedStatement selectAssetClassStmt;
selectAllAssetClassesStmt = cassandraDatasource.getSession().prepare("select code,description from asset_class where code = ?");
Solution
If you use plain Cassandra (without Spring Data), you should use the 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;
Not directly the matter but why do you declare a PreparedStatement
field ?
It should not be required, it is error prone and it could result to unexpected behavior as you don't know its state when you will use it later.
The PreparedStatement
variable should just be a local variable which the lifespan is a method that does an access to the database.
Answered By - davidxxx
Answer Checked By - Timothy Miller (JavaFixing Admin)