Database Connection Handling: Continuous vs. On-Demand
A typical scenario involves a Minecraft Bukkit plugin requiring a database connection. The question arises: should this connection remain open throughout the plugin's execution or be established and terminated only when necessary?
On-Demand Connection Establishment
Opening database connections repeatedly can be computationally expensive. Instead, it is advisable to create a connection only when it is needed for database operations.
Java provides two approaches for this:
<code class="java">try (Connection con = ...) { // Perform database operations }</code>
Connection Pooling
However, manually managing database connections is still prone to inefficiencies. It is recommended to employ a database connection pool, represented by Java's DataSource interface, which handles physical database connections dynamically.
When a connection is "closed" using Connection#close, it is simply placed in a "sleep" mode and remains open.
Related Resources
Tools for Connection Pooling:
The above is the detailed content of Continuous or On-Demand: How Should Your Minecraft Bukkit Plugin Handle Database Connections?. For more information, please follow other related articles on the PHP Chinese website!