What is a connection in JDBC?

What is a connection object in JDBC? How is this connection supported (I mean this network connection)? Are they TCP / IP connections? Why is it an expensive operation to create a connection every time? Why do these connections become obsolete after a while and do I need to update the pool? Why can't I use one connection to execute multiple requests?

+4
source share
4 answers

These connections are TCP / IP connections. In order not to create overhead with every new connection, there are connection pools that expand and contract dynamically. You can use one connection for several requests. I think you mean that you let him go to the pool. If you do this, you can return the same connection from the pool. In this case, it does not matter if you execute one or more queries

The cost of a connection is a connection that takes some time. The ANd database prepares some things, such as sessions, etc. For each connection. This must be done every time. Compounds become obsolete for several reasons. The most noticeable is the firewall between them. Connection problems may lead to a drop in connection or there may be simple timeouts

+4
source

To add to other answers:

Yes, you can reuse the same connection for multiple requests. This is even advisable, since creating a new connection is quite expensive.

You can even perform multiple queries at once. You just need to use a new instance of java.sql.Statement / PreparedStatement for each request. Statements are what JDBC uses to keep track of current requests, so each concurrent request requires its own expression. However, you can and should reuse instructions for sequential requests.

+2
source

The answers to your questions are that they are an implementation. A JDBC connection is an interface that provides methods. What happens behind the scenes may be what provides the interface. For example, consider the internal Oracle JDBC driver used to support Java stored procedures. Simultaneous requests are not only possible, they are more or less inevitable, since each request for a new connection returns the same connection object. I don’t know for sure if it uses TCP / IP internally, but I doubt it.

Therefore, you should not accept implementation details without knowing which JDBC implementation you are using.

0
source

since I still can’t comment, write the answer, just to comment on Vinegar's answer, the situation with setAutoCommit (), returning to the default state when the connection is returned to the pool, is not required behavior and should not be taken for granted, as well as closing statements and results; you can read that it should be closed, but if you do not close them, they will be automatically closed with the connection closed. Do not take this for granted, as it will use your resources in some versions of jdbc drivers.

We had a serious problem with the DB2 database on the AS400, the guys who needed transactional isolation called connection.setAutoCommit (false), and after shutting down they returned that connection to the pool (JNDI) without connection.setAutoCommit (old_state), so when another thread received this connection from the pool, they did not perform insertions and updates, and no one could understand why for a long time ...

0
source

All Articles