Package tk.airshipcraft.commonlib.db
Class SqlConnectionManager
java.lang.Object
tk.airshipcraft.commonlib.db.SqlConnectionManager
Manages SQL database connections using HikariCP for connection pooling.
This manager is responsible for creating and configuring a pool of database
connections that can be reused for database operations, improving performance
by reducing the overhead of establishing connections for each operation.
- Since:
- 2023-12-27
- Version:
- 1.0.0
- Author:
- notzune
-
Constructor Summary
ConstructorsConstructorDescriptionSqlConnectionManager
(DatabaseConfig config) Initializes a new SqlConnectionManager with the provided database configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoid
beginTransaction
(Connection conn) Begins a new database transaction.void
close()
Closes the data source and releases all pooled connections.void
commitTransaction
(Connection conn) Commits the current transaction.Retrieves a database connection from the connection pool.void
releaseSavepoint
(Connection conn, Savepoint savepoint) Releases a previously set savepoint.void
resetConnection
(Connection conn) Resets the connection to its default auto-commit state.void
Rolls back the current transaction.setSavepoint
(Connection conn, String name) Sets a savepoint in the current transaction.
-
Constructor Details
-
SqlConnectionManager
Initializes a new SqlConnectionManager with the provided database configuration. Sets up a connection pool using HikariCP with the configuration settings specified in the provided DatabaseConfig object.- Parameters:
config
- The database configuration object containing properties for setting up the connection pool.
-
-
Method Details
-
getConnection
Retrieves a database connection from the connection pool. The connection is ready to be used for executing SQL statements.- Returns:
- A
Connection
object representing the database connection. - Throws:
SQLException
- If a database access error occurs or the data source has been closed.
-
close
public void close()Closes the data source and releases all pooled connections. This method should be called when the connection manager is no longer needed, typically at the application shutdown, to ensure that all resources are properly released. -
beginTransaction
Begins a new database transaction.- Parameters:
conn
- The database connection.- Throws:
SQLException
- If an error occurs during setting auto-commit to false.
-
commitTransaction
Commits the current transaction.- Parameters:
conn
- The database connection.- Throws:
SQLException
- If an error occurs during the transaction commit.
-
rollbackTransaction
Rolls back the current transaction.- Parameters:
conn
- The database connection.
-
setSavepoint
Sets a savepoint in the current transaction.- Parameters:
conn
- The database connection.name
- The name of the savepoint.- Returns:
- A Savepoint object.
- Throws:
SQLException
- If an error occurs during setting the savepoint.
-
releaseSavepoint
Releases a previously set savepoint.- Parameters:
conn
- The database connection.savepoint
- The savepoint to release.- Throws:
SQLException
- If an error occurs during releasing the savepoint.
-
resetConnection
Resets the connection to its default auto-commit state.- Parameters:
conn
- The database connection.- Throws:
SQLException
- If an error occurs during setting auto-commit to true.
-