Handling database connections in a standalone application [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a standalone application (no application/web server) with various methods accessing a database via JDBC. So far, a database connection is opened (and closed) in every method. There is no need for multiple connections at the same time.
But:
Creating a new connection every time seems a bad idea because of performance
Alternatively, using a single connection seems a bad idea as well.
What is the way to go? Connection pooling for just one connection?

If you configure it right, you can gain a lot by using a connection pool, most of all the performance of individual statements - connecting to the DB might be measured in seconds.
At the same time except for the initial pool creation (you might be able to run that parallel to other initialization) you still maintain a very good relaibility - as the pool will check connections on checkout or in between and discard connections that broke down. So you're likely to survive episodes of "not-connected" or similar as well.
I share your view that using a single connection might be a bad idea - because you'd have to deal with connection loss / reconnect all over your code.

A couple of benefits you could get from connection pooling, even if you only have 1 connection:
A connection pool typically manages the life cycle of it's connections. For instance, if a connection goes stale, a new one will be created in it's place. That prevents you from having to handle life cycle events in your code.
A connection pool can control the opening and closing of connections. Just because you call close() on a connection, doesn't necessarily mean the connection is closed by the pool. It can choose to keep the connection open. This can offer performance benefits if your application is constantly opening and closing connections.

Do not use connection pooling for only one thread.
For a standard JDBC connection pool, a pool of Connection objects is created at the time the application starts. That is when the connection pool server starts, it creates a predetermined number of Connection objects. These objects are then used by a pool manager which disperses them when they are requested by different clients. And returns them to the pool when the client doesn't need that Connection object. A great deal of resources are involved in managing this.
So it's basically wasting a little performance wise bothways if only one connection is being used throughout. IMO, opening and closing individual Connection objects will be a better option. Try sending batch queries to compensate on the performance loss.

Related

Client-Server architectur for Apps [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I built up my server for my app which is running on Android. And i will hear some opinions from you if the setup is acceptable or complete wrong. So the architecture works like request-respone.
The user will log-in, every app start and will get a new session id.
The client and the server does not have a keep-alive connection. The connection is getting cut every request. For every command on the server, the session id need to get checked first, and then the command or request can be calculated. After everything is done the connection will cut. I was thinking about something that the connection will be held to the server and this gives me the possibility to send from the server some data directly to the client. This could have many usages. But on the other hand its not thread "able". Because i will have to synchronize the socket access and share 1 socket object between all classes and activity, this isnt in my opinion a good way. But am still wondering how other apps or online games could sent data or messages directly to the client. This means that a connection is held. I think that they doing it seperated in a service or something like this. This a new problem im facing. I could use firebase cloud messaging, but this is very slow when more as 100 threads are running on the server. A better solution where, to code a second server program, which is running seprated from the main server and keeps a connection to the client. This would be my solution.
What i just want to know if my architecture is good to go or its a bad idea.
In my opinion, opening and closing the connection is a good practice, because the connection is relatively an expensive resource.
So, I would say that yes, you're good to go with the architecture that you currently have implemented.
Open connection
Execute operation
Close connection

is not closing the connection to the DB harmful [duplicate]

This question already has answers here:
Is there a reason for never closing a JDBC connection?
(3 answers)
Closed 8 years ago.
a question just came to my mind when I noticed that I didn't close the connection to the database in many classes of my java application.
I wonder if this can affect my entire OS as the time goes by.
Thanks in advance.
To the Database, no. Depending on the driver and adaptor for your language, it will either automatically close itself after the request is complete or (most likely) just timeout after a while. It will continue to listen for your application's connection after the application has completed it's requests thus consuming more resources on the server until the DB decides it's time to end that connection.

Closing connections to ports , without the existence of socket bindings to the same in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a small client server app , that involves the client pinging the server on a set of ports ( 90,6100,6200).
I use inet.isReachable(30000) to monitor whether the server responds to the client or not.
In the end , I want to make sure that when I re-run my application , any existing connections to these ports are removed.
How do I go about doing so ?
And is there a way to automatically close a connection when I am using InetAddress, which does not seem to have any method to terminate an existing connection to an address ?
"I want to make sure that when I re-run my application , any existing connections to these ports are removed."
The connections cannot stay open when your application exits - there is no process for them to be connected to. So in that sense, you don't have to worry about closing them on next launch.
"And is there a way to automatically close a connection when I am using InetAddress, which does not seem to have any method to terminate an existing connection to an address ?"
Yes - the connection will close when isReachable is responded to, or when the timeout expires (or some other error occurs). Just creating an InetAddress does not create a connection - it automatically creates and destroys them as needed to do its functionality. Connections are generally just short lived things that don't stay open unless you make them.
The JVM will close the connection after the server responds to isReachable or the timeout expires, whichever comes first.
The answer to your vague question is that as connections don't survive the process they are created by, and as INetAddress"isReachable() doesn't leak connections, there is no actual problem here to solve.

Why we need a connection pooling for JDBC? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
What are the benefits of using a JDBC connection pooling tool like DBCP or c3p0 ?
in case of a small CRUD application with one user, can we just create one connection session as a singleton ?
PS: I'm building a small javafx application back-ended with tiny h2 database (5
tables).
From Jon Skeet's answer to What is the benefit of Connection and Statement Pooling?:
Creating a network connection to a database server is (relatively)
expensive. Likewise asking the server to prepare a SQL statement is
(relatively) expensive.
Using a connection/statement pool, you can reuse existing
connections/prepared statements, avoiding the cost of initiating a
connection, parsing SQL etc.
And the following, from Kent Boogaart's answer:
I am not familiar with c3p0, but the benefits of pooling connections
and statements include:
Performance. Connecting to the database is expensive and slow. Pooled connections can be left physically connected to the database,
and shared amongst the various components that need database access.
That way the connection cost is paid for once and amortized across all
the consuming components.
Diagnostics. If you have one sub-system responsible for connecting to the database, it becomes easier to diagnose and analyze database
connection usage.
Maintainability. Again, if you have one sub-system responsible for handing out database connections, your code will be easier to maintain
than if each component connected to the database itself.
Creating connections is costly and it doesn't make sense to create a new connection for each transaction that might only take a few milliseconds. Managing database connections in a pool means that applications can perform database transactions in a way that avoid the connection creation time (The connections still need to be created, but they are all created at startup). The downside is that if all connections are in use, and another connection is required, the requesting thread will have to wait until a connection is returned to the pool.

What is the use of a Connection pool and is it harmful? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
For Hibernate we can use a Connection pool to manage connections inside of it. Is the state of the connections inside the pool opened? Or is it closed? And if the connections are open is there a possible security threat and a threat to the Database.
And we are using Oracle as the database- so is there a internal mechanism inside Oracle to disconnect unused connections?
The connections inside the pool are open (at least for some time period; depending on your pool implementation idle connections might get closed). Creating and opening new database connections can be expensive. Pooling is used to reduce this cost.
There's really no more security threat with using connection pooling than there would be without. In either case, your application still has the same level of access to the database; the same level of damage can be done regardless of if a connection has to be opened first or not.
The purpose of pooling the database connections is to have a set of open connections so that every time the application tries to open a new connection, the pool transparently returns already opened connection. This is much faster than opening new connection every time.
From the database perspective it looks like your application has open but idle database connection (like if you would open SQL console and not run any queries).
I am not a security expert, also I don't know how secure is Oracle connection and TCP/IP stack. But the fact that the idle connection remains for several seconds between your application shouldn't be a problem. Millions of applications are using database connection pooling (in fact, I can't think of any application not using it) and I have never heard of any attack vector targeting it. Remember that pooled connections are still subject of datbase authorization and authentication.
Consider tunneling or encrypting the database connection if it worries you that much (or if the database connection is over the Internet, not intranet.
All this issues are transparent to the using code. You will make this questions only if you implement a connection pool by yourself. If you use a well known (such as c3p0) you do not get in touch with that issues because you are coding against the DataSource interface.
(That does'nt mean that this libraries are free per-se of bugs, memory leaks or orphane open connections).

Categories

Resources