Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert DataSource to otherdatasource

 
sushi sri
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


i'm using appserver's datasource for my database access.
So, when i do jndi lookup i'm getting Datasource with ctx.lookup("ds");
Now i've change it to SQLServerDataSource.

How can we do this?

Thanks

 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can cast it to SQLServerDataSource since DataSource is just an interface. In other words, all instantiated objects have a concrete class. The object is either a SQLServerDataSource or some other concrete object, regardless of the pointer class.

Keep in mind, you should check for class-safe compatibility by using the instanceof operator.
 
sushi sri
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did like this earlier.

class TestdataSource impements DataSource
{
---
}

Datasource ds = new TestdataSource();
if( ds instanceof SQLServerdatasource)
SQLServerdatasource sds = (SQLServerdatasource)ds;
where SQLServerDataSource implements DataSource.

This is my requirement. when i did like this i got error as known classcastexception.
I know we can't compare interface to class. But thisis my requirement.

Need help
Thanks
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you implementing your own data source, TestdataSource? You rarely make your own data source, other than as a wrapper.

As you have defined it TestdataSource is not an instance of SQLServerdatasource.
 
sushi sri
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response scott.

This is to implement the proxy design pattern which is my cleint requirement.
I've to proceed like this only for the SQLServerDataSource.

OK, if i do testdatasource extends SQLserverDataSource i can achieve want i want.(setting the applicationname).

But i'm having problem in accessing the datasource as specified earlier.
Which is the retrieval of sqlserverdatasource from appserver's datasource.
i changed the type of datasource to sqlserverdatasource and factory to sqlserverdatasourceobjectfactory from BasicDataSourceFactory.
But still having runtime problems which says "javax.naming.NamingException: Invalid DataSource reference"


2. if i can't get SQLServerDataSource from appaserver then, can we do this?

The datasource i'm getting with out any changes to type & factory values to sqlserver is basicdatasource.
Which is instance of basicdatasource.(org.apache.tomcat.dbcp.dbcp.BasicDataSource -- which extendsObject implements Datasource) .
If above is not possible then, can we change basicDataSource to SQLServerDataSource.
If so how can i do that?

Thanks for th



How can we do this?
Any suggestion or help required.

Thnaks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic