HTTP Connection Pooling in GlassFish - java

In my webservice, I need to place some HTTP calls. Is it possible to do some connection pooling, like I do JDBC connection pooling?
In the Admin Console of GlassFish I see the configuration items Connector Connection Pool and Connector Resources. Can I use these?

doekman's answer is one possible approach.
Over in my company, we just use Apache Commons' HTTPClient library, which has its own connection pool manager. This link below should start you off easy.
http://hc.apache.org/httpclient-3.x/performance.html
It's your own value judgement whether or not you want to pull in another external dependency. Having migrated our applications from Tomcat, we chose to retain the dependency on HTTPClient just 'cos it's easy to use while alleviating the need to build (and maintain) another factory class.

No. For HTTP you don't actually need connection pooling (except if you are a browser). A HTTP connection is much cheaper than a database connection.
However, You can use a custom resource, so you can configure the connection in JNDI. This article helped me out. There are also three follow up posts.

This blog post and whitepaper on GlassFish Performance Tuning may help:
John Clingan
GlassFish Group Product Manager
Sun Microsystems

Related

Pass Database Connection from GWT App to Java Program

I have a web app built in GWT running in tomcat that accesses the database. On the same server, I have some server-side Java applications running that need to use the same database connection. These applications will be deployed as a war file alongside the GWT app.
Is there a way I can send the connection from the GWT app to these server-side apps?
This isn't a whole lot of information, I understand. To me, the problem is easy enough to understand, though I haven't found a solution yet. If you need anymore information, I'll be happy to provide.
Do you mean, that the applications have to use the same jdbc connection string or is it more like a shared connection pool.
Is it a solution for you to have a datasource in the local jndi and use this in your applications as well as in the servlets?
Actually this has nothing to do with GWT.
Have a look at this answer
ServletContext provides a way of sharing objects among different servlets and even among diffrenet webapps
this.getServletConfig().getServletContext().setAttribute("sharedObject", shared);
this.getServletConfig().getServletContext().getAttribute("sharedObject");
You cannot use the same connection in two different web apps. But if you mean to use same connection pool form which both the GWT and the other web app will fetch connections, then you need to configure the datasource at the application server level using JNDI
There are lots of JNDI tutorials using servlet in the internet.
Following is a link I found on using JNDI in GWT
http://humblecode.blogspot.in/2009/05/gwt-16-using-jndi-datasource.html

Use tomcat to bind to LDAP for more than authentication?

I have a tomcat server that uses the JNDI Directory Realm to bind to an LDAP server to do authentication and this works fine.
However, I'd like to pull more information from the LDAP server (e.g., name, phone number, email, etc) and wondering what is the best way to do this? So, I see there are two possibilites:
1) Is there a way to piggy-back on this already configured Realm? If so, how?
2) Or do I have to make a separate LDAP connection? If so, can I just the LDAP libraries and routines already in tomcat?
Thanks for the help!
If you need to bind to an LDAP server for retrieving information, go with plain JNDI, it's the standard API for accessing directories and performing CRUD operations over them, it'll just work even outside of Tomcat.
I doubt any container will allow you access to the LDAP connection. There are provisions in some containers to retrieve entries within LDAP itself using LDAP search queries (say to retrieve groups), but containers are usually not designed to share the connection with a developer.
You can use JNDI like Oscar suggests or a host of other APIs. Use a connection yourself since the spec says nothing about sharing connections with you.
JNDI should not be used for new code. JNDI has very little to recommend it:
JNDI has quite a number of software defects
JNDI does not support the full range of the LDAP standards (and makes certain standard LDAP features impossible to use)
the available examples are horrible
it uses a disconnected parameter setting mechanism
and it uses a deprecated configuration.
The UnboundID LDAP SDK supports sensible constructs for connection and LDAP operations.
see also
LDAP: programming practices
LDAP: search

How to use JDBC Connection Pool in Restlet Java Webservice?

Hi All I am using REST framework to devlope webservice in Java through restlet API.
I am using Tomcat 6.0.35 as web container.
I want to use Database Connection Pool for JDBC connection in my project.
I came to know about BoneCP is good one, so I want to try it.
I have 20 webservices in one project i.e. one Resource class for one webservice. And there is single Application class that routes each web service by clients request.
Right now I am opening and closing the JDBC Connection in every webservice.
So my question is, How can I use one JDBC connection for all webservices?
In which class I have to open and close the JDBC connection?
Please help me.
Thanks in Advance.
I would suggest you use Hibernate with C3P0 connection pooling , its best , it directly gives you sessions and you dont have to worry about closing the connection etc.

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

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 somewhat 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.

JDBC Database Connections in a Web App DAL

I am building a small website for fun/learning using a fairly standard Web/Service/Data Access layered design.
For the Data Access Layer, what is the best way to handle creating Connection objects to call my SQL stored procedures and why? Bearing in mind I am writing a lot of the code by hand (I know I could be using Hibernate etc to do a lot of this for me)...
1) Should I create one static instance of the Connection and run all my querys through it or will this cause concurrency problems?
2) Should I create a Connection instance per database call and accept the performance overhead? (I will look into connection pooling at a later date if this is the case)
You should use one Connection per thread. Don't share connections across threads.
Consider using Apache DBCP. This is a free and standard way of configuring database connections and drawing them from a pool. It's the method used by high-performance web servers like Tomcat.
Furthermore, if you're using DBCP, since it's a pool (read: cached), there's little penalty to creating/closing connections frequently.
The standard way is to set up a DataSource. All application servers are able to do so via their admin console. The pool is then accessible by it's JNDI name (e.g. "jdbc/MyDB").
The data source should, in fact, be a connection pool (and usually is). It caches connections, tests them before passing to the application and does a lot of other important functions.
In your code you:
resolve JNDI name and cast it into DataSource
get a connection from the data source
do your work
close the connection (it goes back to the pool here)
You can set up the pool yourself (using any of freely available pool implementation), but it really doesn't make any sense if you're using an application server.
P.S.
Since it's a web application a good way to make sure you have closed your connection after the request is to use HttpFilter. You can set up one in web.xml. When the request comes, acquire the connection, put it into ThreadLocal. During the request, get the connection from ThreadLocal, but never close it. After the request, in the filter, close the connection.

Categories

Resources