开发者

How to connect to a database configured in glassfish from an external java application?

开发者 https://www.devze.com 2023-01-07 20:37 出处:网络
The title pretty much says it all, but here\'s some background. We have a database configured in glassfish accessed by a website deployed on glassfish, we also access the database from an external ja

The title pretty much says it all, but here's some background.

We have a database configured in glassfish accessed by a website deployed on glassfish, we also access the database from an external java application, it occurred to me that this could be somew开发者_如何学运维hat inefficient since we open quite a few connections to the database from our external app. So I was wondering if we could somehow access he database through glassfish's connection pool?

Thanks in advance.

Piers


You'll have to try this.

http://javahowto.blogspot.com/2006/08/access-glassfish-datasource-remotely.html suggests it is possible to look up the connection pool via JNDI from a standalone client. An actual client code example is given at http://javahowto.blogspot.com/2006/08/access-jboss-datasource-remotely-from.html#client


That would only be possible if your external application was running in the same VM as glassfish. The word "external" says: Nope, not possible.

Note that databases are designed to handle lots of connections. That's one of the main reasons why they exist: To allow several programs at once to work on the same data.


I don't think you can access the pool, if your program does not run on glassfish. A few connections to the database shouldn't matter too much, at least if the aren't created and destroyed very often. You could build a pool in your external application as well, if that is the case.


Well I'm answering my own question, and it's not a brilliant answer but it does sort of fulfil the requirements of my question.

What you can do is connect to an EJB from an external java application, if the bean provides access to your database then by using it you will be making use of the connection pooling provided by glassfish.

You can find info on how to do this

here - https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html

In terms of efficiency I'm not sure if this is a good idea since it may put excess strain on your web application, in my initial idea I was thinking to bypass the webapp completely and communicate directly with glassfish, any comments on this would be appreciated.


Another option is to add a web service to your Glassfish data access object.

You could then access the database with SOAP or REST calls.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号