Class CqlSessionFactoryBean
java.lang.Object
org.springframework.data.cassandra.config.CqlSessionFactoryBean
- All Implemented Interfaces:
DisposableBean,FactoryBean<com.datastax.oss.driver.api.core.CqlSession>,InitializingBean,PersistenceExceptionTranslator
- Direct Known Subclasses:
CassandraCqlSessionFactoryBean,CassandraSessionFactoryBean
public class CqlSessionFactoryBean
extends Object
implements FactoryBean<com.datastax.oss.driver.api.core.CqlSession>, InitializingBean, DisposableBean, 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
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final intprotected final LogFields inherited from interface org.springframework.beans.factory.FactoryBean
OBJECT_TYPE_ATTRIBUTE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected com.datastax.oss.driver.api.core.CqlSessionBuilderprotected com.datastax.oss.driver.api.core.CqlSessionbuildSession(com.datastax.oss.driver.api.core.CqlSessionBuilder sessionBuilder) Build aSessionto the user-defined Keyspace or the default Keyspace if the user did not specify a Keyspace byname.protected com.datastax.oss.driver.api.core.CqlSessionbuildSystemSession(com.datastax.oss.driver.api.core.CqlSessionBuilder sessionBuilder) Build the CassandraSystem Session.protected voidClose the regular session object.protected voidClose the system session object.protected voidcreateTables(boolean drop, boolean dropUnused, boolean ifNotExists) Perform schema actions.voiddestroy()protected StringGets the name of the Cassandra Keyspace to connect to.protected CassandraMappingContextcom.datastax.oss.driver.api.core.CqlSessionClass<? extends com.datastax.oss.driver.api.core.CqlSession>protected com.datastax.oss.driver.api.core.CqlSessionReturns a reference to the connected CassandraCqlSession.Deprecated.Deprecated.booleanNull-safe operation to determine whether the CassandraCqlSessionis connected or not.protected voidPerform the configuredSchemaActionusingCassandraMappingContextmetadata.voidsetContactPoints(String contactPoints) Set a comma-delimited string of the contact points (hosts) to connect to.voidsetContactPoints(Collection<InetSocketAddress> contactPoints) Set a collection of the contact points (hosts) to connect to.voidsetConverter(CassandraConverter converter) Deprecated.voidsetKeyspaceActions(List<KeyspaceActions> keyspaceActions) Set aListofKeyspaceActionsto be executed on initialization.voidsetKeyspaceAlterations(List<AlterKeyspaceSpecification> specifications) voidsetKeyspaceCreations(List<CreateKeyspaceSpecification> specifications) voidsetKeyspaceDrops(List<DropKeyspaceSpecification> specifications) voidsetKeyspaceName(String keyspaceName) Sets the name of the Cassandra Keyspace to connect to.voidsetKeyspaceShutdownScripts(List<String> scripts) Set aListof rawCQL statementsthat are executed in the scope of the system keyspace when this factory isdestroyed.voidsetKeyspaceSpecifications(List<? extends KeyspaceActionSpecification> keyspaceSpecifications) voidsetKeyspaceStartupScripts(List<String> scripts) Set aListof rawCQL statementsthat are executed in the scope of the system keyspace when this factory isinitialized.voidsetLocalDatacenter(String localDatacenter) Sets the name of the local datacenter.voidsetPassword(String password) Set the password to use.voidsetPort(int port) Set the port for the contact points.voidsetSchemaAction(SchemaAction schemaAction) Deprecated.voidsetSessionBuilderConfigurer(SessionBuilderConfigurer sessionBuilderConfigurer) Sets theSessionBuilderConfigurerto configure theSessionBuilder.voidsetShutdownScripts(List<String> scripts) Deprecated.voidsetStartupScripts(List<String> scripts) Deprecated.voidsetSuspendLifecycleSchemaRefresh(boolean suspendLifecycleSchemaRefresh) Set whether to suspend schema refresh settings duringafterPropertiesSet()anddestroy()lifecycle callbacks.voidsetUsername(String username) Set the username to use.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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
-
-
Constructor Details
-
CqlSessionFactoryBean
public CqlSessionFactoryBean()
-
-
Method Details
-
isConnected
public boolean isConnected()Null-safe operation to determine whether the CassandraCqlSessionis connected or not.- Returns:
- a boolean value indicating whether the Cassandra
CqlSessionis 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 simplehostto 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 addressesto 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 theCassandraConverterto use. Schema actions will derive table and user type information from theCassandraMappingContextinsideconverter.- Parameters:
converter- must not be null.
-
getConverter
- Returns:
- the configured
CassandraConverter.
-
setKeyspaceActions
Set aListofKeyspaceActionsto be executed on initialization. Keyspace actions may contain create and drop specifications.- Parameters:
keyspaceActions- theListofKeyspaceActions.
-
getKeyspaceActions
- Returns:
- the
ListofKeyspaceActions.
-
setKeyspaceAlterations
Set aListofalter keyspace specificationsthat are executed when this factory isinitialized.Alter keyspace specificationsare executed on a system session with no keyspace set, before executingsetStartupScripts(List).- Parameters:
specifications- theListofcreate keyspace specifications.
-
setKeyspaceCreations
Set aListofcreate keyspace specificationsthat are executed when this factory isinitialized.Create keyspace specificationsare executed on a system session with no keyspace set, before executingsetStartupScripts(List).- Parameters:
specifications- theListofcreate keyspace specifications.
-
setKeyspaceDrops
Set aListofdrop keyspace specificationsthat are executed when this factory isdestroyed.Drop keyspace specificationsare executed on a system session with no keyspace set, before executingsetShutdownScripts(List).- Parameters:
specifications- theListofdrop 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- TheKeyspaceActionSpecificationto set.
-
getKeyspaceSpecifications
- Returns:
- the
KeyspaceActionSpecificationassociated with this factory.
-
setKeyspaceStartupScripts
Set aListof rawCQL statementsthat 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 aListof rawCQL statementsthat are executed in the scope of the system keyspace when this factory isdestroyed.Drop keyspace specificationsare 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_UNUSEDand mapping-based schema creation rely on schema metadata.- Parameters:
suspendLifecycleSchemaRefresh-trueto suspend the schema refresh during lifecycle callbacks;falseotherwise 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 CassandraCqlSessionwas not properly initialized.- See Also:
-
CqlSession
-
setSessionBuilderConfigurer
public void setSessionBuilderConfigurer(@Nullable SessionBuilderConfigurer sessionBuilderConfigurer) Sets theSessionBuilderConfigurerto 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:
afterPropertiesSetin interfaceInitializingBean
-
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-CqlSessionBuilderused to a build a CassandraCqlSession.- Returns:
- the built Cassandra
System Session. - See Also:
-
CqlSessionBuilderCqlSession
-
buildSession
protected com.datastax.oss.driver.api.core.CqlSession buildSession(com.datastax.oss.driver.api.core.CqlSessionBuilder sessionBuilder) Build aSessionto the user-defined Keyspace or the default Keyspace if the user did not specify a Keyspace byname.- Parameters:
sessionBuilder-CqlSessionBuilderused to a build a CassandraCqlSession.- Returns:
- the built
CqlSessionto the user-defined Keyspace. - See Also:
-
CqlSessionBuilderCqlSession
-
performSchemaAction
protected void performSchemaAction()Perform the configuredSchemaActionusingCassandraMappingContextmetadata. -
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 EXISTSto each creation statement.
-
getObject
public com.datastax.oss.driver.api.core.CqlSession getObject()- Specified by:
getObjectin interfaceFactoryBean<com.datastax.oss.driver.api.core.CqlSession>
-
getObjectType
- Specified by:
getObjectTypein interfaceFactoryBean<com.datastax.oss.driver.api.core.CqlSession>
-
translateExceptionIfPossible
- Specified by:
translateExceptionIfPossiblein interfacePersistenceExceptionTranslator
-
destroy
public void destroy()- Specified by:
destroyin interfaceDisposableBean
-
closeSession
protected void closeSession()Close the regular session object. -
closeSystemSession
protected void closeSystemSession()Close the system session object.
-
SessionFactoryInitializerorSessionFactoryFactoryBeanwithKeyspacePopulatorinstead.