I have one application in Java Swing.
In which i have used one connection for the application on single database.
But now multiple users are usig that application , so everytime new connection is getting created.
DB is coming very slow in performance;
Can I use connection pooling in Swing based desktop based application.
DB used is SQL server 2000.
Any help appriciated.
Thanx in advance.
Yes you can use C3P0
There are many connection pooling libraries in fact.
http://commons.apache.org/pool/
might be also helpful. I actually had the same question for CouchDB here:
Connection pool for couchdb
Related
Hi I have Java application which connects to different databases like DB2, MySQL etc. One way I can think of is in a configuration file we configure databases and create different JDBC Connection objects and connect to the databases.
Is this the best way to achieve this task? Or we can achieve this in a better way with minumum efficient code.
Please guide. Thanks in advance.
Put the details of databases, connections strings, drivers etc in a configuration/properties file.
Read that property file on application startup or on first database connection request
Implement a factory of connections and use typeofDB as the input to the factory, which will return a connection object for that database using the configuration.
You can control the number of connections etc in your factory if you want.
Hope it helps!
I am responsible for maintaining a bunch of legacy code which involves several deployed servlets; some through Tomcat 5.5 (yes I know - I did say legacy code!) and others through Glassfish 3.3. All connect using the ojdbc14 driver, to Oracle 10 databases. All use pooled connections.
However, occasionally, the connection to the DB is dropped, the DB restarts, etc., and the servlets fail until someone notices and they are re-deployed. Not. Cool. I understand there is some way to monitor the DB connection the servlet makes, so that, in the event of a lost connection, the servlet 'knows' - how do I accomplish this? And, can I make the servlet smart enough to recover itself by reinstigating the DB conection? I've searched around for an answer, but I have not been successful. Suggestions and pointers muchly appreciated.. I'm a bit of a newbie at this. Thanks!
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.
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.
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.