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 Details

  • Constructor Details

    • CqlSessionFactoryBean

      public CqlSessionFactoryBean()
  • Method Details

    • isConnected

      public boolean isConnected()
      Null-safe operation to determine whether the Cassandra CqlSession is connected or not.
      Returns:
      a boolean value indicating whether the Cassandra CqlSession is connected.
      See Also:
    • setContactPoints

      public void setContactPoints(String contactPoints)
      Set a comma-delimited string of the contact points (hosts) to connect to. Default is localhost; see DEFAULT_CONTACT_POINTS. Contact points may use the form host:port, or a simple host to use the configured port.
      Parameters:
      contactPoints - the contact points used by the new cluster, must not be null.
    • setContactPoints

      public void setContactPoints(Collection<InetSocketAddress> contactPoints)
      Set a collection of the contact points (hosts) to connect to. Default is localhost; see DEFAULT_CONTACT_POINTS.
      Parameters:
      contactPoints - the contact points used by the new cluster, must not be null. Use unresolved addresses to delegate hostname resolution to the driver.
      Since:
      3.1
    • setLocalDatacenter

      public void setLocalDatacenter(@Nullable String localDatacenter)
      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 is 9042, see DEFAULT_PORT.
      Parameters:
      port - the port used by the new cluster.
    • setUsername

      public void setUsername(String username)
      Set the username to use.
      Parameters:
      username - The username to set.
    • setPassword

      public void setPassword(String password)
      Set the password to use.
      Parameters:
      password - The password to set.
    • setConverter

      @Deprecated public void setConverter(CassandraConverter converter)
      Set the CassandraConverter to use. Schema actions will derive table and user type information from the CassandraMappingContext inside converter.
      Parameters:
      converter - must not be null.
    • getConverter

      @Nullable public CassandraConverter getConverter()
      Returns:
      the configured CassandraConverter.
    • setKeyspaceActions

      public void setKeyspaceActions(List<KeyspaceActions> keyspaceActions)
      Set a List of KeyspaceActions to be executed on initialization. Keyspace actions may contain create and drop specifications.
      Parameters:
      keyspaceActions - the List of KeyspaceActions.
    • getKeyspaceActions

      public List<KeyspaceActions> getKeyspaceActions()
      Returns:
      the List of KeyspaceActions.
    • setKeyspaceAlterations

      public void setKeyspaceAlterations(List<AlterKeyspaceSpecification> specifications)
      Set a List of alter keyspace specifications that are executed when this factory is initialized. Alter keyspace specifications are executed on a system session with no keyspace set, before executing setStartupScripts(List).
      Parameters:
      specifications - the List of create keyspace specifications.
    • setKeyspaceCreations

      public void setKeyspaceCreations(List<CreateKeyspaceSpecification> specifications)
      Set a List of create keyspace specifications that are executed when this factory is initialized. Create keyspace specifications are executed on a system session with no keyspace set, before executing setStartupScripts(List).
      Parameters:
      specifications - the List of create keyspace specifications.
    • setKeyspaceDrops

      public void setKeyspaceDrops(List<DropKeyspaceSpecification> specifications)
      Set a List of drop keyspace specifications that are executed when this factory is destroyed. Drop keyspace specifications are executed on a system session with no keyspace set, before executing setShutdownScripts(List).
      Parameters:
      specifications - the List of drop keyspace specifications.
    • setKeyspaceName

      public void setKeyspaceName(@Nullable String keyspaceName)
      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

      @Nullable protected String 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 - The KeyspaceActionSpecification to set.
    • getKeyspaceSpecifications

      public Set<KeyspaceActionSpecification> getKeyspaceSpecifications()
      Returns:
      the KeyspaceActionSpecification associated with this factory.
    • setKeyspaceStartupScripts

      public void setKeyspaceStartupScripts(List<String> scripts)
      Set a List of raw CQL statements that are executed in the scope of the system keyspace when this factory is initialized. Scripts are executed on a system session with no keyspace set, after executing setKeyspaceCreations(List).
      Parameters:
      scripts - the scripts to execute on startup
    • setKeyspaceShutdownScripts

      public void setKeyspaceShutdownScripts(List<String> scripts)
      Set a List of raw CQL statements that are executed in the scope of the system keyspace when this factory is destroyed. Drop keyspace specifications are executed on a system session with no keyspace set, after executing setKeyspaceDrops(List).
      Parameters:
      scripts - the scripts to execute on shutdown
    • getMappingContext

      protected CassandraMappingContext getMappingContext()
      Returns:
      the CassandraMappingContext.
    • setSchemaAction

      @Deprecated public void setSchemaAction(SchemaAction schemaAction)
      Set the SchemaAction.
      Parameters:
      schemaAction - must not be null.
    • getSchemaAction

      public SchemaAction getSchemaAction()
      Returns:
      the SchemaAction.
    • setSuspendLifecycleSchemaRefresh

      public void setSuspendLifecycleSchemaRefresh(boolean suspendLifecycleSchemaRefresh)
      Set whether to suspend schema refresh settings during afterPropertiesSet() and destroy() lifecycle callbacks. Disabled by default to use schema metadata settings of the session configuration. When enabled (set to true), 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 Cassandra CqlSession.
      Returns:
      a reference to the connected Cassandra CqlSession.
      Throws:
      IllegalStateException - if the Cassandra CqlSession was not properly initialized.
      See Also:
      • CqlSession
    • setSessionBuilderConfigurer

      public void setSessionBuilderConfigurer(@Nullable SessionBuilderConfigurer sessionBuilderConfigurer)
      Sets the SessionBuilderConfigurer to configure the SessionBuilder.
      Parameters:
      sessionBuilderConfigurer -
    • setStartupScripts

      @Deprecated public void setStartupScripts(@Nullable List<String> scripts)
      Sets CQL scripts to be executed immediately after the session is connected.
    • getStartupScripts

      @Deprecated public List<String> getStartupScripts()
      Returns an unmodifiable list of startup scripts.
    • setShutdownScripts

      @Deprecated public void setShutdownScripts(@Nullable List<String> scripts)
      Sets CQL scripts to be executed immediately before the session is shutdown.
    • getShutdownScripts

      @Deprecated public List<String> getShutdownScripts()
      Returns an unmodifiable list of shutdown scripts.
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.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 Cassandra System Session.
      Parameters:
      sessionBuilder - CqlSessionBuilder used to a build a Cassandra CqlSession.
      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 a Session to the user-defined Keyspace or the default Keyspace if the user did not specify a Keyspace by name.
      Parameters:
      sessionBuilder - CqlSessionBuilder used to a build a Cassandra CqlSession.
      Returns:
      the built CqlSession to the user-defined Keyspace.
      See Also:
      • CqlSessionBuilder
      • CqlSession
    • performSchemaAction

      protected void performSchemaAction()
      Perform the configured SchemaAction using CassandraMappingContext 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 the CassandraMappingContext).
      ifNotExists - true to perform fail-safe creations by adding IF NOT EXISTS to each creation statement.
    • getObject

      public com.datastax.oss.driver.api.core.CqlSession getObject()
      Specified by:
      getObject in interface org.springframework.beans.factory.FactoryBean<com.datastax.oss.driver.api.core.CqlSession>
    • getObjectType

      public Class<? extends com.datastax.oss.driver.api.core.CqlSession> getObjectType()
      Specified by:
      getObjectType in interface org.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 interface org.springframework.dao.support.PersistenceExceptionTranslator
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • closeSession

      protected void closeSession()
      Close the regular session object.
    • closeSystemSession

      protected void closeSystemSession()
      Close the system session object.