From 5154d2b5316ea8323817f7fad3cd38a06eff6346 Mon Sep 17 00:00:00 2001 From: Stefan Birkner Date: Sun, 19 Jun 2016 22:54:10 +0200 Subject: [PATCH] DATACASS-305 - Improve readability of CassandraCqlClusterFactoryBean.executeSpecsAndScripts. We now check first whether any CQL statements need to be executed. This makes the code simpler because we don't have to check whether the session has already been created. Rename the Session variable system to session to omit confusion. Remove CQL logging here as CQL statements are logged inside the CqlTemplate. Original pull request: #70. --- .../CassandraCqlClusterFactoryBean.java | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java b/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java index 5e137b851..fd03da4aa 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java @@ -269,43 +269,27 @@ public class CassandraCqlClusterFactoryBean protected void executeSpecsAndScripts(@SuppressWarnings("rawtypes") List specs, List scripts) { - Session system = null; + if (!CollectionUtils.isEmpty(specs) || !CollectionUtils.isEmpty(scripts)) { + Session session = cluster.connect(); - try { - if (!CollectionUtils.isEmpty(specs)) { - system = cluster.connect(); - - CqlTemplate template = new CqlTemplate(system); + try { + CqlTemplate template = new CqlTemplate(session); for (Object spec : specs) { String cql = (spec instanceof CreateKeyspaceSpecification) ? new CreateKeyspaceCqlGenerator((CreateKeyspaceSpecification) spec).toCql() : new DropKeyspaceCqlGenerator((DropKeyspaceSpecification) spec).toCql(); - if (log.isDebugEnabled()) { - log.debug("executing raw CQL [{}]", cql); - } - template.execute(cql); } - } - - if (!CollectionUtils.isEmpty(scripts)) { - system = (system != null ? system : cluster.connect()); - - CqlTemplate template = new CqlTemplate(system); for (String script : scripts) { - if (log.isDebugEnabled()) { - log.debug("executing raw CQL [{}]", script); - } - template.execute(script); } - } - } finally { - if (system != null) { - system.close(); + } finally { + if (session != null) { + session.close(); + } } } }