This may seem just like a noob question but this is actually the very first time I am treading into Database territory.
From here I acquired the data that
The best method to implement communication between your server and database is to setup a database connection pool. Developing a new connection for every client request can be very time-consuming, specifically for programs that continuously receive a lot of demands.
and also the tutorial utilizes a JNDI datasource.
My application can also be similar(however i will not be utilising Tomcat, just electrical sockets) and my server will be receiving demands from multiple clients however i do not understand why must I personally use a JNDI datasource, why can't the server maintain one open reference to the Database so when a customer request arrives it'll process the request and feed the information towards the client.
Within the worst situation, Basically should require a JNDI how do i implement it with my server application?
Thus, it's a client application? The applying and also the database usually talks with one another utilizing a connection acquired by
DriverManager#getConnection()? If that's the case, then you definitely don't always need JNDI to obtain connection pooling to operate. Alone the bond pooling framework under consideration would already suffice. For instance C3P0 or Apache Commons DBCP (I would suggest C3P0 DBCP is singlethreaded). Just replace the
DriverManager#getConnection() because of it.
Edit: reply in your comments:
The server could be speaking towards the database and also the clients connect with the server, and so i will not know whether or not to refer to this as a customer application.
I really mean, an ordinary vanilla Java application which does not run in the Java EE container. Pascal has phrased it better.
Really I am a bit unclear about how connection pooling works, does each connection run in the own thread? can there be any document/book that helped me to obtain a better knowledge of these concepts vis-a-vis a non put connection?
To begin, the bond pool opens an association and holds it open as lengthy as as much as the set up timeout. The bond pool systems/decorates the bond using its own implementation. The bond pool can open and hold a set up quantity of connections concurrently. Whenever you call
getConnection(), it'll immediately provide you with a previously opened up connection. Whenever you call
close() around the connection, it'll place the connection in the pool for future demands. This thus implies that you've still got to create the JDBC code the typical way: acquire and close the
ResultSet within the least possible scope. Close all of them within the
finally block. In case your JDBC code has already been well crafted, actually only the
DriverManager#getConnection() must be changed. As you need to open and shut the
Connection in the identical method block, it'll normally run within the same thread. The bond pooling will be worried about the
Connection isn't acquired by another threads in the intervening time until your code calls
close() around the
You'll find here a pleasant article to obtain the idea how connection pooling works underneath the hood (be mindful: don't apply it production and do not homegrow it further, it is only to obtain the whole idea). Legitimate work, make use of an existing completely developed and robust connection pooling framework.