This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Data Cassandra 4.4.0! |
Migration Guide from 2.x to 3.x
Spring Data for Apache Cassandra 3.0 introduces a set of breaking changes when upgrading from earlier versions.
Review dependencies
Upgrading to Spring Data Cassandra requires an upgrade to the DataStax Driver version 4. Upgrading to the new driver comes with transitive dependency changes, most notably, Google Guava is bundled and shaded by the driver. Check out the DataStax Java Driver for Apache Cassandra 4 Upgrade Guide for details on the Driver-related changes.
Adapt Configuration
DataStax Java Driver 4 merges Cluster
and Session
objects into a single CqlSession
object, therefore, all Cluster
-related API was removed.
The configuration was revised in large parts by removing most configuration items that were moved into DriverConfigLoader
that is mostly file-based.
This means that SocketOptions
, AddressTranslator
and many more options are configured now through other means.
If you’re using XML-based configuration, make sure to migrate all configuration files from the cql
namespace (www.springframework.org/schema/cql www.springframework.org/schema/cql/spring-cql.xsd
) to the cassandra
namespace (www.springframework.org/schema/data/cassandra www.springframework.org/schema/data/cassandra/spring-cassandra.xsd
).
To reflect the change in configuration builders, ClusterBuilderConfigurer
was renamed to SessionBuilderConfigurer
accepting now CqlSessionBuilder
instead of the Cluster.Builder
.
Make sure to also provide the local data center in your configuration as it is required to properly configure load balancing.
Connectivity
The configuration elements for Cluster
(cassandra:cluster
) and Session
(cassandra:session
) were merged into a single CqlSession
(cassandra:session
) element that configures both, the keyspace and endpoints.
With the upgrade, schema support was moved to a new namespace element: cassandra:session-factory
that provides a SessionFactory
bean.
<cassandra:cluster contact-points="localhost" port="9042">
<cassandra:keyspace action="CREATE_DROP" name="mykeyspace" />
</cassandra:cluster>
<cassandra:session keyspace-name="mykeyspace" schema-action="CREATE">
<cassandra:startup-cql>CREATE TABLE …</cassandra:startup-cql>
</cassandra:session>
<cassandra:session contact-points="localhost" port="9042" keyspace="mykeyspace" local-datacenter="datacenter1">
<cassandra:keyspace action="CREATE_DROP" name="mykeyspace" />
</cassandra:session>
<cassandra:session-factory schema-action="CREATE">
<cassandra:script location="classpath:/schema.cql"/>
</cassandra:session-factory>
Spring Data Cassandra 3.0 no longer registers default Mapping Context, Context and Template API beans when using XML namespace configuration. The defaulting should be applied on application or Spring Boot level. |
Template API
Spring Data for Apache Cassandra encapsulates most of the changes that come with the driver upgrade as the Template API and repository support if your application mainly interacts with mapped entities or primitive Java types.
We generally recommend to create CqlTemplate
and CassandraTemplate
objects by using SessionFactory
as the factory usage allows synchronization for schema creation and introduces a level of flexibility when working with multiple databases.
<cql:template session-ref="…" />
<cassandra:template session-ref="…" cassandra-converter-ref="…"/>
<cassandra:session-factory />
<cassandra:cql-template session-factory-ref="…" />
<cassandra:template session-factory-ref="…" cassandra-converter-ref="…"/>
You will have to adapt your code in all places, where you use DataStax driver API directly. Typical cases include:
-
Implementations of
ResultSetExtractor
-
Implementations of
RowCallbackHandler
-
Implementations of
RowMapper
-
Implementations of
PreparedStatementCreator
including async and reactive variants -
Calls to
CqlTemplate.queryForResultSet(…)
-
Calling methods that accept
Statement
Changes in AsyncCqlTemplate
DataStax driver 4 has changed the result type of queries that are run asynchronously. To reflect these changes, you need to adapt your code that provides:
-
Implementations of
AsyncSessionCallback
-
Implementations of
AsyncPreparedStatementCreator
Result set extraction requires a new interface for DataStax' AsyncResultSet
.
AsyncCqlTemplate
now uses AsyncResultSetExtractor
in places where it used previously ResultSetExtractor
.
Note that AsyncResultSetExtractor.extractData(…)
returns a Future
instead of a scalar object so a migration of code comes with the possibility to use fully non-blocking code in the extractor.
Data model migrations
Your data model may require updates if you use the following features:
-
@CassandraType
-
forceQuote
in@Table
,@Column
,@PrimaryKeyColumn
,@PrimaryKey
and@UserDefinedType
-
Properties using
java.lang.Date
-
Properties using
UDTValue
orTupleValue
@CassandraType
DataStax driver 4 no longer ships with a Name
enumeration to describe the Cassandra type.
We decided to re-introduce the enumeration with CassandraType.Name
.
Make sure to update your imports to use the newly introduced replacement type.
Other changes
-
Driver’s
ConsistencyLevel
constant class was removed and reintroduced asDefaultConsistencyLevel
.@Consistency
was adapted toDefaultConsistencyLevel
. -
RetryPolicy
onQueryOptions
and…CqlTemplate
types was removed without replacement. -
Drivers’s
PagingState
type was removed. Paging state now usesByteBuffer
. -
SimpleUserTypeResolver
acceptsCqlSession
instead ofCluster
. -
SimpleTupleTypeFactory
was migrated toenum
.SimpleTupleTypeFactory.INSTANCE
no longer requires aCluster
/CqlSession
context. -
Introduction of
StatementBuilder
to functionally build statements as the QueryBuilder API uses immutable statement types. -
Session
bean renamed fromsession
tocassandraSession
andSessionFactory
bean renamed fromsessionFactory
tocassandraSessionFactory
. -
ReactiveSession
bean renamed fromreactiveSession
toreactiveCassandraSession
andReactiveSessionFactory
bean renamed fromreactiveSessionFactory
toreactiveCassandraSessionFactory
. -
ReactiveSessionFactory.getSession()
now returns aMono<ReactiveSession>
. Previously it returned justReactiveSession
. -
Data type resolution was moved into
ColumnTypeResolver
so allDataType
-related methods were moved fromCassandraPersistentEntity
/CassandraPersistentProperty
intoColumnTypeResolver
(affected methods areMappingContext.getDataType(…)
,CassandraPersistentProperty.getDataType()
,CassandraPersistentEntity.getUserType()
, andCassandraPersistentEntity.getTupleType()
). -
Schema creation was moved from
MappingContext
toSchemaFactory
(affected methods areCassandraMappingContext.getCreateTableSpecificationFor(…)
,CassandraMappingContext.getCreateIndexSpecificationsFor(…)
, andCassandraMappingContext.getCreateUserTypeSpecificationFor(…)
).
Deprecations
-
CassandraCqlSessionFactoryBean
, useCqlSessionFactoryBean
instead. -
KeyspaceIdentifier
andCqlIdentifier
, usecom.datastax.oss.driver.api.core.CqlIdentifier
instead. -
CassandraSessionFactoryBean
, useCqlSessionFactoryBean
instead. -
AbstractCqlTemplateConfiguration
, useAbstractSessionConfiguration
instead. -
AbstractSessionConfiguration.getClusterName()
, useAbstractSessionConfiguration.getSessionName()
instead. -
CodecRegistryTupleTypeFactory
, useSimpleTupleTypeFactory
instead. -
Spring Data’s
CqlIdentifier
, use the driverCqlIdentifier
instead. -
forceQuote
attributes as quoting is no longer required.CqlIdentifier
properly escapes reserved keywords and takes care of case-sensitivity. -
fetchSize
onQueryOptions
and…CqlTemplate
types was deprecated, usepageSize
instead -
CassandraMappingContext.setUserTypeResolver(…)
,CassandraMappingContext.setCodecRegistry(…)
, andCassandraMappingContext.setCustomConversions(…)
: Configure these properties onCassandraConverter
. -
TupleTypeFactory
andCassandraMappingContext.setTupleTypeFactory(…)
:TupleTypeFactory
is no longer used as the Cassandra driver ships with aDataTypes.tupleOf(…)
factory method. -
Schema creation via
CqlSessionFactoryBean
(cassandra:session
) is deprecated. Keyspace creation viaCqlSessionFactoryBean
(cassandra:session
) is not affected.
Removals
Configuration API
-
PoolingOptionsFactoryBean
-
SocketOptionsFactoryBean
-
CassandraClusterFactoryBean
-
CassandraClusterParser
-
CassandraCqlClusterFactoryBean
-
CassandraCqlClusterParser
-
CassandraCqlSessionParser
-
AbstractClusterConfiguration
-
ClusterBuilderConfigurer
(useSessionBuilderConfigurer
instead
Utilities
-
GuavaListenableFutureAdapter
-
QueryOptions
andWriteOptions
constructor takingConsistencyLevel
andRetryPolicy
arguments. Use the builder in conjunction of execution profiles as replacement. -
CassandraAccessor.setRetryPolicy(…)
andReactiveCqlTemplate.setRetryPolicy(…)
methods. Use execution profiles as replacement.
Namespace support
-
cql
namespace (www.springframework.org/schema/cql
, usewww.springframework.org/schema/data/cassandra
instead) -
cassandra:cluster
(endpoint properties merged tocassandra:session
) -
cql:template
, usecassandra:cql-template
instead -
Removed implicit bean registrations Mapping Context, Context and Template API beans. These must be declared explicitly.