# ClassCastException when using Liquibase Facade

**URL:** <https://forum.liquibase.org/t/classcastexception-when-using-liquibase-facade/501>\
**Category:** General Discussion\
**Created:** [April 9, 2010, 7:57pm UTC](https://forum.liquibase.org/t/classcastexception-when-using-liquibase-facade/501 "2010-04-09T19:57:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarat.pediredla](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@sarat.pediredla](https://forum.liquibase.org/u/sarat.pediredla)\
**Post date:** [April 9, 2010, 7:57pm UTC](https://forum.liquibase.org/t/classcastexception-when-using-liquibase-facade/501/1 "2010-04-09T19:57:00Z")

</div>

Hi,

I am trying to invoke Liquibase through the Facade as described in [http://liquibase.jira.com/wiki/display/CONTRIB/Liquibase+Facade](http://liquibase.jira.com/wiki/display/CONTRIB/Liquibase+Facade) by passing it a dataSource created by DBCP through Spring as follows,

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 

I am using 2.0 RC1.

In my class, I am invoking Liquibase as follows,

// Create the database  
ApplicationContext ctx = WebApplicationContextUtils  
.getWebApplicationContext(ServletActionContext  
.getServletContext());  
BasicDataSource dataSource = ((BasicDataSource) ctx  
.getBean(“dataSource”));

```
	Connection connection = DataSourceUtils
			.getTargetConnection(DataSourceUtils.getConnection(dataSource));

	// Create the database
	Liquibase liquibase = new Liquibase("db.xml",
			new ClassLoaderResourceAccessor(), DatabaseFactory
					.getInstance().findCorrectDatabaseImplementation(
							(DatabaseConnection) connection));
	liquibase.update(null);

```

The problem here is that when using DBCP, it becomes nigh on impossible to cast the connection to a DatabaseConnection and I get a ClassCastException.

I have tried using DriverManager to re-construct a datasource but it still fails noting that it just can’t cast whatever connection is returned by JDBC to DatabaseConnection. I can’t see a way to “create” a DatabaseConnection class in the documentation at all.

Creating another JNDI data source might work but it isn’t an option as I dont want to define 2 different datasources which our end users have to configure.

Any help from people who have experience or insight is appreciated.

---

<div class="post-metadata">

**Author:** ![nvoxland](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@nvoxland](https://forum.liquibase.org/u/nvoxland)\
**Post date:** [April 9, 2010, 7:57pm UTC](https://forum.liquibase.org/t/classcastexception-when-using-liquibase-facade/501/2 "2010-04-09T19:57:00Z")

</div>

The documentation hasn’t been updated with some of the changes made in 2.0 yet.

You can call “new JdbcConnection(conn)” to correctly wrap your connection object to pass to the facade.&nbsp; I’ll probably create a constructor on liquibase.Liquibase to take a standard connection object as well.

Nathan

---

<div class="post-metadata">

**Author:** ![sarat.pediredla](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@sarat.pediredla](https://forum.liquibase.org/u/sarat.pediredla)\
**Post date:** [April 9, 2010, 7:57pm UTC](https://forum.liquibase.org/t/classcastexception-when-using-liquibase-facade/501/3 "2010-04-09T19:57:00Z")

</div>

Thanks Nathan! This was keeping me up for ages. I am happy to volunteer to update documentation if it helps as Liquibase is soon going to become a core necessity for our work.

---

<div class="post-metadata">

**Author:** ![nvoxland](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@nvoxland](https://forum.liquibase.org/u/nvoxland)\
**Post date:** [April 9, 2010, 7:57pm UTC](https://forum.liquibase.org/t/classcastexception-when-using-liquibase-facade/501/4 "2010-04-09T19:57:00Z")

</div>

Any help you could do would be greatly appreciated.&nbsp; I set up the documentation as a wiki so it can be easily updated as people find errors or holes.

Nathan
