aspose file tools*
The moose likes JDBC and the fly likes Accessing DataSource outside container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Accessing DataSource outside container " Watch "Accessing DataSource outside container " New topic
Author

Accessing DataSource outside container

Rahul Chaitanya
Greenhorn

Joined: Aug 22, 2010
Posts: 22

Hello,

I wrote a standalone java application and this application needs to access the DB through the Data Sources that were established in Web Sphere. This application will not be deployed in Web Sphere but i will have to get the connection properties or connections from Web Sphere Data Sources itself.

All, I need was the connection properties..so that I can use them and connect from my standalone application. If I can access the data sources and get the connection that is well and good and if that is not possible is there any way that I can get the connection properties from the server and use in my java application to connect to the data base ?

Please, help.

Thanks.
Rahul Chaitanya
Greenhorn

Joined: Aug 22, 2010
Posts: 22
So far.. I was able to write the following code: "Idea is to build the websphere context through java code and then access its datasource".

Properties prop = new Properties();

prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

prop.put(Context.PROVIDER_URL,"iiop://localhost:2811");

InitialContext init = new InitialContext(prop);

DataSource ds = (DataSource) init.lookup("jdbc/testDS");

But the problem is.. I was not able to identify the IIOP port number to use in the above code ? Does anybody has any idea about finding the iiop port number ?

Rahul Chaitanya
Greenhorn

Joined: Aug 22, 2010
Posts: 22
Well..I was able to identify the port number in the following manner:

FROM WAS ADMIN CONSOLE: SERVERS -> APPLICATION SERVERS -> Under Communications -> Expand Ports

Now, I am getting an other error when I am trying to get the connection:

Properties prop = new Properties();

prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
prop.put(Context.PROVIDER_URL,"iiop://localhost:2811");

InitialContext init = new InitialContext(prop);

DataSource ds = (DataSource) init.lookup("jdbc/testDS");

//Connection conn = ds.getConnection("USER NAME","PASSWORD");
Connection conn = ds.getConnection();

If, I give the username/pwd while retrieving the connection it is working but it is not working if i don't give them. Since, I am thinking that it is not possible to get data source of websphere externally without authentication. But I guess setting up the authentication alias will be able to solve the problem. I tried it to but no luck.

Please, let me know if anybody has any information in retrieving the data source through custom properties/authentication alias ??

Thanks.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

You don't really expect to use that data source without providing the authentication details, do you? And when you do provide those details, you can access the data source successfully. I would say you have a solution already.
Rahul Chaitanya
Greenhorn

Joined: Aug 22, 2010
Posts: 22
Thank you very much for your reply Paul.

But the idea is to have the DB information in one place and have all the application to use it.

So..I will have to grab the connection properties from websphere data source without providing the user credentials and so I build the websphere context and did a look up for the JNDI. But was not able to get the connection without providing the user credentials.

Is there any other way where I can get the user credentials from the data source itself ?

I heard that establishing authentication properties through custom properties might solve the problem but I tried establishing them with names: user and password. But even that approach failed.

Any suggestions will be greatly appreciated.

Thanks,
Kireet Pola.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

It appears you have completely missed the point of securing the data source. If it were possible for any application to use the data source without providing any security credentials, there would be no point in having those security credentials. So a solution in which your application connects to the data source and presents the credentials is perfectly normal. Solutions in which you somehow get hold of the data source without doing that do not exist.
Rahul Chaitanya
Greenhorn

Joined: Aug 22, 2010
Posts: 22
Actually, authentication alias doesn't seems to be getting applied and so it is expecting me to provide those details specifically. I completely agree with you but my requirement is to grab the connection from the websphere itself since, the idea is to maintain the db information in 1 place.

Please, let me know if there is any other way for retrieving the DB credentials from the data source itself.

Thanks.
sangmin li
Greenhorn

Joined: Feb 11, 2012
Posts: 1
which version of WebSphere Application Server do you use?
which version of Data source do you use?
Rahul Chaitanya
Greenhorn

Joined: Aug 22, 2010
Posts: 22
Actually, we are using WAS 6.1 and I am not sure about the version of DS. But if there is a solution then I will be really happy to know about it although we decided to not to go over this approach of accessing data ource for getting the connections to a standalone application.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing DataSource outside container