/** * Prepare the given CQL Statement (or {@link com.datastax.driver.core.PreparedStatement}), applying statement * settings such as retry policy and consistency level. * * @param statement the CQL Statement to prepare * @see #setRetryPolicy(RetryPolicy) * @see #setConsistencyLevel(ConsistencyLevel) */ protected <T extends PreparedStatement> T applyStatementSettings(T statement) { ConsistencyLevel consistencyLevel = getConsistencyLevel(); if (consistencyLevel != null) { statement.setConsistencyLevel(consistencyLevel); } RetryPolicy retryPolicy = getRetryPolicy(); if (retryPolicy != null) { statement.setRetryPolicy(retryPolicy); } return statement; }
/** * Translate the given {@link DriverException} into a generic {@link DataAccessException}. * <p> * The returned {@link DataAccessException} is supposed to contain the original {@code DriverException} as root cause. * However, client code may not generally rely on this due to {@link DataAccessException}s possibly being caused by * other resource APIs as well. That said, a {@code getRootCause() instanceof DataAccessException} check (and * subsequent cast) is considered reliable when expecting Cassandra-based access to have happened. * * @param task readable text describing the task being attempted * @param cql CQL query or update that caused the problem (may be {@literal null}) * @param ex the offending {@link DriverException} * @return the DataAccessException, wrapping the {@code DriverException} * @see org.springframework.dao.DataAccessException#getRootCause() * @see <a href= * "http://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#dao-exceptions">Consistent * exception hierarchy</a> */ protected DataAccessException translate(String task, @Nullable String cql, DriverException ex) { Assert.notNull(ex, "DriverException must not be null"); return getExceptionTranslator().translate(task, cql, ex); }
/** * Returns the Cassandra {@link Session} from {@link SessionFactory} used by this template to perform Cassandra data * access operations. * * @return the Cassandra {@link Session} used by this template. * @see com.datastax.driver.core.Session * @deprecated since 2.0. This class uses a {@link SessionFactory} to dispatch CQL calls amongst different * {@link Session}s during its lifecycle. */ @Deprecated public Session getSession() { Assert.state(getSessionFactory() != null, "SessionFactory was not properly initialized"); return getSessionFactory().getSession(); }
/** * Prepare the given CQL Statement (or {@link com.datastax.driver.core.PreparedStatement}), applying statement * settings such as fetch size, retry policy, and consistency level. * * @param statement the CQL Statement to prepare * @see #setFetchSize(int) * @see #setRetryPolicy(RetryPolicy) * @see #setConsistencyLevel(ConsistencyLevel) */ protected <T extends Statement> T applyStatementSettings(T statement) { ConsistencyLevel consistencyLevel = getConsistencyLevel(); if (consistencyLevel != null && statement.getConsistencyLevel() == DEFAULTS.getConsistencyLevel()) { statement.setConsistencyLevel(consistencyLevel); } int fetchSize = getFetchSize(); if (fetchSize != -1 && statement.getFetchSize() == DEFAULTS.getFetchSize()) { statement.setFetchSize(fetchSize); } RetryPolicy retryPolicy = getRetryPolicy(); if (retryPolicy != null && statement.getRetryPolicy() == DEFAULTS.getRetryPolicy()) { statement.setRetryPolicy(retryPolicy); } return statement; }
@SuppressWarnings("ConstantConditions") private int getEffectiveFetchSize(Statement statement) { if (statement.getFetchSize() > 0) { return statement.getFetchSize(); } if (getCqlOperations() instanceof CassandraAccessor) { CassandraAccessor accessor = (CassandraAccessor) getCqlOperations(); if (accessor.getFetchSize() != -1) { return accessor.getFetchSize(); } } return getCqlOperations().execute(this::getConfiguredFetchSize); }
/** * Sets the Cassandra {@link Session} used by this template to perform Cassandra data access operations. The * {@code session} will replace the current {@link #getSessionFactory()} with {@link DefaultSessionFactory}. * * @param session Cassandra {@link Session} used by this template, must not be{@literal null}. * @see com.datastax.driver.core.Session * @see DefaultSessionFactory */ public void setSession(Session session) { Assert.notNull(session, "Session must not be null"); setSessionFactory(new DefaultSessionFactory(session)); }
@SuppressWarnings("ConstantConditions") private Mono<Integer> getEffectiveFetchSize(Statement statement) { if (statement.getFetchSize() > 0) { return Mono.just(statement.getFetchSize()); } if (getReactiveCqlOperations() instanceof CassandraAccessor) { CassandraAccessor accessor = (CassandraAccessor) getReactiveCqlOperations(); if (accessor.getFetchSize() != -1) { return Mono.just(accessor.getFetchSize()); } } return getReactiveCqlOperations().execute((ReactiveSessionCallback<Integer>) session -> Mono .just(session.getCluster().getConfiguration().getQueryOptions().getFetchSize())).single(); }
@SuppressWarnings("ConstantConditions") private int getEffectiveFetchSize(Statement statement) { if (statement.getFetchSize() > 0) { return statement.getFetchSize(); } if (getAsyncCqlOperations() instanceof CassandraAccessor) { CassandraAccessor accessor = (CassandraAccessor) getAsyncCqlOperations(); if (accessor.getFetchSize() != -1) { return accessor.getFetchSize(); } } return getAsyncCqlOperations() .execute((AsyncSessionCallback<Integer>) session -> AsyncResult.forValue(getConfiguredFetchSize(session))) .completable().join(); }
/** * Translate the given {@link DriverException} into a generic {@link DataAccessException}. * <p> * The returned {@link DataAccessException} is supposed to contain the original {@code DriverException} as root cause. * However, client code may not generally rely on this due to {@link DataAccessException}s possibly being caused by * other resource APIs as well. That said, a {@code getRootCause() instanceof DataAccessException} check (and * subsequent cast) is considered reliable when expecting Cassandra-based access to have happened. * * @param ex the offending {@link DriverException} * @return the DataAccessException, wrapping the {@code DriverException} * @see <a href= * "http://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#dao-exceptions">Consistent * exception hierarchy</a> * @see DataAccessException */ @Nullable protected DataAccessException translateExceptionIfPossible(DriverException ex) { Assert.notNull(ex, "DriverException must not be null"); return getExceptionTranslator().translateExceptionIfPossible(ex); }