11package io .goodforgod .testcontainers .extensions .cassandra ;
22
3- import com .datastax .oss .driver .api .core .CqlIdentifier ;
43import com .datastax .oss .driver .api .core .CqlSession ;
54import com .datastax .oss .driver .api .core .CqlSessionBuilder ;
65import com .datastax .oss .driver .api .core .config .DriverConfigLoader ;
@@ -176,15 +175,13 @@ private CqlSession openConnection() {
176175 .withCodecRegistry (new DefaultCodecRegistry ("testing-codec-registry" ))
177176 .withConfigLoader (configLoader )
178177 .withLocalDatacenter (params ().datacenter ())
179- .addContactPoint (new InetSocketAddress (params ().host (), params ().port ()));
178+ .addContactPoint (InetSocketAddress . createUnresolved (params ().host (), params ().port ()));
180179
181180 if (params ().username () != null && params ().password () != null ) {
182181 sessionBuilder .withAuthCredentials (params ().username (), params ().password ());
183182 }
184183
185- CqlSession session = sessionBuilder .build ();
186- createKeyspace (params ().keyspace (), session );
187- return session ;
184+ return sessionWithKeyspace (params ().keyspace (), sessionBuilder );
188185 }
189186
190187 @ Override
@@ -208,13 +205,24 @@ public void createKeyspace(@NotNull String keyspaceName) {
208205 createKeyspace (keyspaceName , getConnection ());
209206 }
210207
208+ private CqlSession sessionWithKeyspace (@ NotNull String keyspaceName , CqlSessionBuilder sessionBuilder ) {
209+ try (var session = sessionBuilder .build ()) {
210+ String cql = "CREATE KEYSPACE IF NOT EXISTS " + keyspaceName
211+ + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};" ;
212+ var boundStatement = session .prepare (cql ).bind ().setTimeout (TIMEOUT );
213+ session .execute (boundStatement ).wasApplied ();
214+ return sessionBuilder .withKeyspace (keyspaceName ).build ();
215+ } catch (Exception e ) {
216+ throw new CassandraConnectionException (e );
217+ }
218+ }
219+
211220 private void createKeyspace (@ NotNull String keyspaceName , CqlSession session ) {
212221 try {
213222 String cql = "CREATE KEYSPACE IF NOT EXISTS " + keyspaceName
214223 + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};" ;
215224 var boundStatement = session .prepare (cql ).bind ().setTimeout (TIMEOUT );
216225 session .execute (boundStatement ).wasApplied ();
217- session .execute ("USE " + CqlIdentifier .fromCql (keyspaceName ));
218226 } catch (Exception e ) {
219227 throw new CassandraConnectionException (e );
220228 }
0 commit comments