Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot cast CassandraConnection #242

Closed
VincentBrule opened this issue Nov 20, 2023 · 6 comments
Closed

Cannot cast CassandraConnection #242

VincentBrule opened this issue Nov 20, 2023 · 6 comments

Comments

@VincentBrule
Copy link

VincentBrule commented Nov 20, 2023

Hello,

I tried to use the latest version of this driver (4.10.2) with the latest version of liquibase-core and liquibase-cassandra (4.25.0). I use SBT (1.9.7) and Java 11.

I haven't managed to replace the old datastax driver with the new one. This is not possible to cast a CassandraConnection into a liquibase.database.jvm.JdbcConnection and vice versa.

Previously, settings were made as follows:

val database = DatabaseFactory.getInstance.findCorrectDatabaseImplementation(
   new JdbcConnection(
      new Driver().connect(
          s"jdbc:cassandra://localhost:${cqlPort.toString}/$keyspace;DefaultKeyspace=$keyspace",
          new Properties()
     )
  )
)
    
new Liquibase(
  changeLog.getAbsolutePath,
  new FileSystemResourceAccessor(new File("/")),
  database
)

After swapping with the ING driver, I updated the above code like this (https://docs.liquibase.com/workflows/liquibase-community/using-liquibase-java-api.html):

val connection: Connection = DriverManager.getConnection(
        s"jdbc:cassandra://localhost:${cqlPort.toString}/$keyspace?compliancemode=Liquibase&localdatacenter=dc1&requesttimeout=10000"
 )

new Liquibase(
  changeLog.getAbsolutePath,
  new FileSystemResourceAccessor(new File("/")),
  DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection))
)

And I'm getting the error:

java.lang.ClassCastException: class liquibase.database.jvm.JdbcConnection cannot be cast to class com.ing.data.cassandra.jdbc.CassandraConnection (liquibase.database.jvm.JdbcConnection and com.ing.data.cassandra.jdbc.CassandraConnection are in unnamed module of loader sbt.internal.LayeredClassLoader @20850fc2)

The stack trace is:

at liquibase.ext.cassandra.database.CassandraDatabase.getKeyspace(CassandraDatabase.java:114)
	at liquibase.ext.cassandra.database.CassandraDatabase.getDefaultCatalogName(CassandraDatabase.java:136)
	at liquibase.changelog.ChangeLogParameters.<init>(ChangeLogParameters.java:74)
	at liquibase.Liquibase.<init>(Liquibase.java:96)
	at vincent.cassandra.LiquibaseUtils$.initializeLiquibase(LiquibaseUtils.scala:102)
	at vincent.cassandra.LiquibaseUtils$.$anonfun$runner$2(LiquibaseUtils.scala:84)
	at vincent.cassandra.LiquibaseUtils$.$anonfun$runner$2$adapted(LiquibaseUtils.scala:83)
	at vincent.cassandra.LiquibaseUtils$.withDatabaseAndChangelog(LiquibaseUtils.scala:123)
	at vincent.cassandra.LiquibaseUtils$.$anonfun$runner$1(LiquibaseUtils.scala:83)
	at liquibase.Scope.lambda$child$0(Scope.java:184)
	at liquibase.Scope.child(Scope.java:193)
	at liquibase.Scope.child(Scope.java:183)
	at liquibase.Scope.child(Scope.java:162)
	at vincent.cassandra.LiquibaseUtils$.runUpdateLiquibase(LiquibaseUtils.scala:70)
	at vincent.cassandra.CassandraSpec.beforeAll(CassandraSpec.scala:86)
	at vincent.cassandra.CassandraSpec.beforeAll$(CassandraSpec.scala:70)
	at vincent.cassandra.LiquibaseUtilsSpecContext1.beforeAll(LiquibaseUtilsSpec.scala:19)
	at org.scalatest.BeforeAndAfterAll.liftedTree1$1(BeforeAndAfterAll.scala:212)
	at org.scalatest.BeforeAndAfterAll.run(BeforeAndAfterAll.scala:210)
	at org.scalatest.BeforeAndAfterAll.run$(BeforeAndAfterAll.scala:208)
	at vincent.cassandra.LiquibaseUtilsSpecContext1.run(LiquibaseUtilsSpec.scala:19)
	at org.scalatest.tools.Framework.org$scalatest$tools$Framework$$runSuite(Framework.scala:321)
	at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:517)
	at sbt.TestRunner.runTest$1(TestFramework.scala:153)
	at sbt.TestRunner.run(TestFramework.scala:168)
	at sbt.TestFramework$$anon$3$$anonfun$$lessinit$greater$1.$anonfun$apply$1(TestFramework.scala:336)
	at sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:296)
	at sbt.TestFramework$$anon$3$$anonfun$$lessinit$greater$1.apply(TestFramework.scala:336)
	at sbt.TestFramework$$anon$3$$anonfun$$lessinit$greater$1.apply(TestFramework.scala:336)
	at sbt.TestFunction.apply(TestFramework.scala:348)
	at sbt.Tests$.$anonfun$toTask$1(Tests.scala:436)
	at sbt.std.Transform$$anon$3.$anonfun$apply$2(Transform.scala:47)
	at sbt.std.Transform$$anon$4.work(Transform.scala:69)
	at sbt.Execute.$anonfun$submit$2(Execute.scala:283)
	at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:24)
	at sbt.Execute.work(Execute.scala:292)
	at sbt.Execute.$anonfun$submit$1(Execute.scala:283)
	at sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:265)
	at sbt.CompletionService$$anon$2.call(CompletionService.scala:65)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
@maximevw
Copy link
Contributor

maximevw commented Nov 20, 2023

Hello,

I found the issue in CassandraDatabase and I think the line 114 should be re-written like this:

if (this.getConnection() instanceof JdbcConnection) {
   keyspace = ((CassandraConnection) ((JdbcConnection) this.getConnection()).getUnderlyingConnection()).getSchema();
}

I don't know if it's really possible that getConnection() directly returns a CassandraConnection.

I tried this solution locally, and it seems to work (using Liquibase in a Spring Boot application with JDK 17).

Please note this issue probably makes this version of liquibase-cassandra unusable for most users.

@pgehl
Copy link

pgehl commented Nov 22, 2023

Please note this issue probably makes this version of liquibase-cassandra unusable for most users.

I confirm I'm one of those users :)
I use liquibase-cassandra with spring-boot and spring-data on java 17.

maximevw added a commit to maximevw/liquibase-cassandra that referenced this issue Nov 23, 2023
Cast the connection to JdbcConnection, then the underlying connection to CassandraConnection
to retrieve the keyspace.
@maximevw
Copy link
Contributor

Hello, I created this PR #243 to fix the issue.
I hope this could be released soon by the Liquibase team.

filipelautert pushed a commit that referenced this issue Dec 7, 2023
Cast the connection to JdbcConnection, then the underlying connection to CassandraConnection
to retrieve the keyspace.
@maximevw
Copy link
Contributor

maximevw commented Dec 7, 2023

@VincentBrule @pgehl FYI, a version 4.25.0.1 has been released to fix this issue.

@filipelautert
Copy link
Contributor

If this issue is not fixed please reopen it.

@VincentBrule
Copy link
Author

Sorry for the delay, everything is working fine, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants