Class CqlSessionFactoryBean
java.lang.Object
org.springframework.data.cassandra.config.CqlSessionFactoryBean
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean
,org.springframework.beans.factory.FactoryBean<com.datastax.oss.driver.api.core.CqlSession>
,org.springframework.beans.factory.InitializingBean
,org.springframework.dao.support.PersistenceExceptionTranslator
- Direct Known Subclasses:
CassandraCqlSessionFactoryBean
,CassandraSessionFactoryBean
public class CqlSessionFactoryBean
extends Object
implements org.springframework.beans.factory.FactoryBean<com.datastax.oss.driver.api.core.CqlSession>, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean, org.springframework.dao.support.PersistenceExceptionTranslator
Factory for creating and configuring a Cassandra
CqlSession
, which is a thread-safe singleton. As such, it is
sufficient to have one CqlSession
per application and keyspace.- Since:
- 3.0
- Author:
- Alex Shvid, Matthew T. Adams, John Blum, Mark Paluch, Tomasz Lelek, Ammar Khaku
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final String
static final int
protected final org.apache.commons.logging.Log
Fields inherited from interface org.springframework.beans.factory.FactoryBean
OBJECT_TYPE_ATTRIBUTE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
protected com.datastax.oss.driver.api.core.CqlSessionBuilder
protected com.datastax.oss.driver.api.core.CqlSession
buildSession
(com.datastax.oss.driver.api.core.CqlSessionBuilder sessionBuilder) Build aSession
to the user-defined Keyspace or the default Keyspace if the user did not specify a Keyspace byname
.protected com.datastax.oss.driver.api.core.CqlSession
buildSystemSession
(com.datastax.oss.driver.api.core.CqlSessionBuilder sessionBuilder) Build the CassandraSystem Session
.protected void
Close the regular session object.protected void
Close the system session object.protected void
createTables
(boolean drop, boolean dropUnused, boolean ifNotExists) Perform schema actions.void
destroy()
protected String
Gets the name of the Cassandra Keyspace to connect to.protected CassandraMappingContext
com.datastax.oss.driver.api.core.CqlSession
Class<? extends com.datastax.oss.driver.api.core.CqlSession>
protected com.datastax.oss.driver.api.core.CqlSession
Returns a reference to the connected CassandraCqlSession
.Deprecated.Deprecated.boolean
Null-safe operation to determine whether the CassandraCqlSession
is connected or not.protected void
Perform the configuredSchemaAction
usingCassandraMappingContext
metadata.void
setContactPoints
(String contactPoints) Set a comma-delimited string of the contact points (hosts) to connect to.void
setContactPoints
(Collection<InetSocketAddress> contactPoints) Set a collection of the contact points (hosts) to connect to.void
setConverter
(CassandraConverter converter) Deprecated.void
setKeyspaceActions
(List<KeyspaceActions> keyspaceActions) Set aList
ofKeyspaceActions
to be executed on initialization.void
setKeyspaceAlterations
(List<AlterKeyspaceSpecification> specifications) void
setKeyspaceCreations
(List<CreateKeyspaceSpecification> specifications) void
setKeyspaceDrops
(List<DropKeyspaceSpecification> specifications) void
setKeyspaceName
(String keyspaceName) Sets the name of the Cassandra Keyspace to connect to.void
setKeyspaceShutdownScripts
(List<String> scripts) Set aList
of rawCQL statements
that are executed in the scope of the system keyspace when this factory isdestroyed
.void
setKeyspaceSpecifications
(List<? extends KeyspaceActionSpecification> keyspaceSpecifications) void
setKeyspaceStartupScripts
(List<String> scripts) Set aList
of rawCQL statements
that are executed in the scope of the system keyspace when this factory isinitialized
.void
setLocalDatacenter
(String localDatacenter) Sets the name of the local datacenter.void
setPassword
(String password) Set the password to use.void
setPort
(int port) Set the port for the contact points.void
setSchemaAction
(SchemaAction schemaAction) Deprecated.void
setSessionBuilderConfigurer
(SessionBuilderConfigurer sessionBuilderConfigurer) Sets theSessionBuilderConfigurer
to configure theSessionBuilder
.void
setShutdownScripts
(List<String> scripts) Deprecated.void
setStartupScripts
(List<String> scripts) Deprecated.void
setSuspendLifecycleSchemaRefresh
(boolean suspendLifecycleSchemaRefresh) Set whether to suspend schema refresh settings duringafterPropertiesSet()
anddestroy()
lifecycle callbacks.void
setUsername
(String username) Set the username to use.org.springframework.dao.DataAccessException
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.beans.factory.FactoryBean
isSingleton
-
Field Details
-
CASSANDRA_SYSTEM_SESSION
- See Also:
-
DEFAULT_CONTACT_POINTS
- See Also:
-
DEFAULT_PORT
public static final int DEFAULT_PORT- See Also:
-
log
protected final org.apache.commons.logging.Log log
-
-
Constructor Details
-
CqlSessionFactoryBean
public CqlSessionFactoryBean()
-
-
Method Details
-
isConnected
public boolean isConnected()Null-safe operation to determine whether the CassandraCqlSession
is connected or not.- Returns:
- a boolean value indicating whether the Cassandra
CqlSession
is connected. - See Also:
-
AsyncAutoCloseable.isClosed()
getObject()
-
setContactPoints
Set a comma-delimited string of the contact points (hosts) to connect to. Default islocalhost
; seeDEFAULT_CONTACT_POINTS
. Contact points may use the formhost:port
, or a simplehost
to use the configuredport
.- Parameters:
contactPoints
- the contact points used by the new cluster, must not be null.
-
setContactPoints
Set a collection of the contact points (hosts) to connect to. Default islocalhost
; seeDEFAULT_CONTACT_POINTS
.- Parameters:
contactPoints
- the contact points used by the new cluster, must not be null. Useunresolved addresses
to delegate hostname resolution to the driver.- Since:
- 3.1
-
setLocalDatacenter
Sets the name of the local datacenter.- Parameters:
localDatacenter
- a String indicating the name of the local datacenter.
-
setPort
public void setPort(int port) Set the port for the contact points. Default is9042
, seeDEFAULT_PORT
.- Parameters:
port
- the port used by the new cluster.
-
setUsername
Set the username to use.- Parameters:
username
- The username to set.
-
setPassword
Set the password to use.- Parameters:
password
- The password to set.
-
setConverter
Deprecated.Set theCassandraConverter
to use. Schema actions will derive table and user type information from theCassandraMappingContext
insideconverter
.- Parameters:
converter
- must not be null.
-
getConverter
- Returns:
- the configured
CassandraConverter
.
-
setKeyspaceActions
Set aList
ofKeyspaceActions
to be executed on initialization. Keyspace actions may contain create and drop specifications.- Parameters:
keyspaceActions
- theList
ofKeyspaceActions
.
-
getKeyspaceActions
- Returns:
- the
List
ofKeyspaceActions
.
-
setKeyspaceAlterations
Set aList
ofalter keyspace specifications
that are executed when this factory isinitialized
.Alter keyspace specifications
are executed on a system session with no keyspace set, before executingsetStartupScripts(List)
.- Parameters:
specifications
- theList
ofcreate keyspace specifications
.
-
setKeyspaceCreations
Set aList
ofcreate keyspace specifications
that are executed when this factory isinitialized
.Create keyspace specifications
are executed on a system session with no keyspace set, before executingsetStartupScripts(List)
.- Parameters:
specifications
- theList
ofcreate keyspace specifications
.
-
setKeyspaceDrops
Set aList
ofdrop keyspace specifications
that are executed when this factory isdestroyed
.Drop keyspace specifications
are executed on a system session with no keyspace set, before executingsetShutdownScripts(List)
.- Parameters:
specifications
- theList
ofdrop keyspace specifications
.
-
setKeyspaceName
Sets the name of the Cassandra Keyspace to connect to. Passing null will cause the Cassandra System Keyspace to be used.- Parameters:
keyspaceName
- a String indicating the name of the Keyspace in which to connect.- See Also:
-
getKeyspaceName
Gets the name of the Cassandra Keyspace to connect to.- Returns:
- the name of the Cassandra Keyspace to connect to as a String.
- See Also:
-
setKeyspaceSpecifications
public void setKeyspaceSpecifications(List<? extends KeyspaceActionSpecification> keyspaceSpecifications) - Parameters:
keyspaceSpecifications
- TheKeyspaceActionSpecification
to set.
-
getKeyspaceSpecifications
- Returns:
- the
KeyspaceActionSpecification
associated with this factory.
-
setKeyspaceStartupScripts
Set aList
of rawCQL statements
that are executed in the scope of the system keyspace when this factory isinitialized
. Scripts are executed on a system session with no keyspace set, after executingsetKeyspaceCreations(List)
.- Parameters:
scripts
- the scripts to execute on startup
-
setKeyspaceShutdownScripts
Set aList
of rawCQL statements
that are executed in the scope of the system keyspace when this factory isdestroyed
.Drop keyspace specifications
are executed on a system session with no keyspace set, after executingsetKeyspaceDrops(List)
.- Parameters:
scripts
- the scripts to execute on shutdown
-
getMappingContext
- Returns:
- the
CassandraMappingContext
.
-
setSchemaAction
Deprecated.Set theSchemaAction
.- Parameters:
schemaAction
- must not be null.
-
getSchemaAction
- Returns:
- the
SchemaAction
.
-
setSuspendLifecycleSchemaRefresh
public void setSuspendLifecycleSchemaRefresh(boolean suspendLifecycleSchemaRefresh) Set whether to suspend schema refresh settings duringafterPropertiesSet()
anddestroy()
lifecycle callbacks. Disabled by default to use schema metadata settings of the session configuration. When enabled (set totrue
), then schema refresh during lifecycle methods is suspended until finishing schema actions to avoid periodic schema refreshes for each DDL statement.Suspending schema refresh can be useful to delay schema agreement until the entire schema is created. Note that disabling schema refresh may interfere with schema actions.
SchemaAction.RECREATE_DROP_UNUSED
and mapping-based schema creation rely on schema metadata.- Parameters:
suspendLifecycleSchemaRefresh
-true
to suspend the schema refresh during lifecycle callbacks;false
otherwise to retain the session schema refresh configuration.- Since:
- 2.7
-
getSession
protected com.datastax.oss.driver.api.core.CqlSession getSession()Returns a reference to the connected CassandraCqlSession
.- Returns:
- a reference to the connected Cassandra
CqlSession
. - Throws:
IllegalStateException
- if the CassandraCqlSession
was not properly initialized.- See Also:
-
CqlSession
-
setSessionBuilderConfigurer
public void setSessionBuilderConfigurer(@Nullable SessionBuilderConfigurer sessionBuilderConfigurer) Sets theSessionBuilderConfigurer
to configure theSessionBuilder
.- Parameters:
sessionBuilderConfigurer
-
-
setStartupScripts
Deprecated.Sets CQL scripts to be executed immediately after the session is connected. -
getStartupScripts
Deprecated.Returns an unmodifiable list of startup scripts. -
setShutdownScripts
Deprecated.Sets CQL scripts to be executed immediately before the session is shutdown. -
getShutdownScripts
Deprecated.Returns an unmodifiable list of shutdown scripts. -
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-
buildBuilder
protected com.datastax.oss.driver.api.core.CqlSessionBuilder buildBuilder() -
buildSystemSession
protected com.datastax.oss.driver.api.core.CqlSession buildSystemSession(com.datastax.oss.driver.api.core.CqlSessionBuilder sessionBuilder) Build the CassandraSystem Session
.- Parameters:
sessionBuilder
-CqlSessionBuilder
used to a build a CassandraCqlSession
.- Returns:
- the built Cassandra
System Session
. - See Also:
-
CqlSessionBuilder
CqlSession
-
buildSession
protected com.datastax.oss.driver.api.core.CqlSession buildSession(com.datastax.oss.driver.api.core.CqlSessionBuilder sessionBuilder) Build aSession
to the user-defined Keyspace or the default Keyspace if the user did not specify a Keyspace byname
.- Parameters:
sessionBuilder
-CqlSessionBuilder
used to a build a CassandraCqlSession
.- Returns:
- the built
CqlSession
to the user-defined Keyspace. - See Also:
-
CqlSessionBuilder
CqlSession
-
performSchemaAction
protected void performSchemaAction()Perform the configuredSchemaAction
usingCassandraMappingContext
metadata. -
createTables
protected void createTables(boolean drop, boolean dropUnused, boolean ifNotExists) Perform schema actions.- Parameters:
drop
- true to drop types/tables.dropUnused
- true to drop unused types/tables (i.e. types/tables not known to be used by theCassandraMappingContext
).ifNotExists
- true to perform fail-safe creations by addingIF NOT EXISTS
to each creation statement.
-
getObject
public com.datastax.oss.driver.api.core.CqlSession getObject()- Specified by:
getObject
in interfaceorg.springframework.beans.factory.FactoryBean<com.datastax.oss.driver.api.core.CqlSession>
-
getObjectType
- Specified by:
getObjectType
in interfaceorg.springframework.beans.factory.FactoryBean<com.datastax.oss.driver.api.core.CqlSession>
-
translateExceptionIfPossible
@Nullable public org.springframework.dao.DataAccessException translateExceptionIfPossible(RuntimeException e) - Specified by:
translateExceptionIfPossible
in interfaceorg.springframework.dao.support.PersistenceExceptionTranslator
-
destroy
public void destroy()- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
-
closeSession
protected void closeSession()Close the regular session object. -
closeSystemSession
protected void closeSystemSession()Close the system session object.
-
SessionFactoryInitializer
orSessionFactoryFactoryBean
withKeyspacePopulator
instead.