Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CityDoctor
CityDoctor2
Commits
6b95778b
Commit
6b95778b
authored
Oct 30, 2025
by
Luna Riegel
Browse files
Feat: Add connection pooling to DatabaseHandler
parent
58a625c9
Changes
1
Hide whitespace changes
Inline
Side-by-side
CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/database/DatabaseHandler.java
View file @
6b95778b
package
de.hft.stuttgart.citydoctor2.database
;
import
com.zaxxer.hikari.HikariDataSource
;
import
de.hft.stuttgart.citydoctor2.check.CheckableUtilsVisitor
;
import
de.hft.stuttgart.citydoctor2.datastructure.BoundingBox
;
import
de.hft.stuttgart.citydoctor2.datastructure.CityObject
;
import
de.hft.stuttgart.citydoctor2.datastructure.Geometry
;
import
de.hft.stuttgart.citydoctor2.datastructure.GmlId
;
import
javafx.scene.control.Alert
;
import
org.apache.commons.io.FilenameUtils
;
import
org.apache.commons.lang3.SerializationException
;
import
org.apache.commons.lang3.SerializationUtils
;
import
org.apache.logging.log4j.LogManager
;
...
...
@@ -15,7 +16,6 @@ import org.h2gis.functions.factory.H2GISDBFactory;
import
org.h2gis.utilities.wrapper.ConnectionWrapper
;
import
org.h2gis.utilities.wrapper.DataSourceWrapper
;
import
javax.sql.DataSource
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
...
...
@@ -27,20 +27,29 @@ import java.util.ArrayList;
import
java.util.List
;
import
java.util.Map
;
public
class
DatabaseHandler
{
private
static
final
Logger
logger
=
LogManager
.
getLogger
(
DatabaseHandler
.
class
);
private
static
final
String
DB_NAME
=
"/database/cd_db"
;
private
DataSourceWrapper
dataSource
;
//TODO: Connection pooling
public
DatabaseHandler
(){
try
{
// Cleanup hook Runtime.getRuntime().addShutdownHook(new Thread(() -> {}));
dataSource
=
new
DataSourceWrapper
(
H2GISDBFactory
.
createDataSource
(
DB_NAME
,
true
,
";AUTO_SERVER=TRUE
;MV_STORE=TRUE;CACHE_SIZE=65536;LOCK_TIMEOUT=10000"
)
);
String
jdbcurl
=
H2GISDBFactory
.
createDataSource
(
DB_NAME
,
true
,
";AUTO_SERVER=TRUE
"
).
getConnection
().
getMetaData
().
getURL
(
);
HikariDataSource
ds
=
new
HikariDataSource
();
setupFeaturesTable
();
ds
.
setJdbcUrl
(
jdbcurl
);
ds
.
setUsername
(
"sa"
);
ds
.
setPassword
(
"sa"
);
int
coreCount
=
Runtime
.
getRuntime
().
availableProcessors
();
ds
.
setMaximumPoolSize
(
coreCount
*
5
);
ds
.
setMinimumIdle
(
coreCount
*
5
);
dataSource
=
new
DataSourceWrapper
(
ds
);
}
catch
(
Exception
e
)
{
logger
.
fatal
(
"FATAL ERROR: Creation of embedded database failed!"
);
logger
.
fatal
(
e
.
getMessage
());
...
...
@@ -78,7 +87,7 @@ public class DatabaseHandler {
* @param co the CityObject
*/
public
void
marshallCityObject
(
CityObject
co
)
{
try
(
Connection
con
=
H2GISDBFactory
.
openSpatialDataBase
(
DB_NAME
))
{
try
(
Connection
con
=
dataSource
.
getConnection
(
))
{
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
"MERGE INTO features VALUES (?, ?, ?)"
))
{
ps
.
setString
(
1
,
co
.
getGmlId
().
toString
());
String
bbox
=
BoundingBox
.
of
(
co
).
to2DWkt
();
...
...
@@ -106,7 +115,7 @@ public class DatabaseHandler {
public
boolean
tryMarshallCityObjectMap
(
Map
<
GmlId
,
CityObject
>
map
)
{
// Id String for error logging
String
id
=
""
;
try
(
Connection
con
=
H2GISDBFactory
.
openSpatialDataBase
(
DB_NAME
))
{
try
(
Connection
con
=
dataSource
.
getConnection
(
))
{
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
"MERGE INTO features VALUES (?, ?, ?)"
))
{
con
.
setAutoCommit
(
false
);
for
(
Map
.
Entry
<
GmlId
,
CityObject
>
entry
:
map
.
entrySet
())
{
...
...
@@ -150,7 +159,7 @@ public class DatabaseHandler {
* @return the Feature, or null if no entry was found
*/
public
CityObject
unmarshallCityObject
(
GmlId
id
)
{
try
(
Connection
con
=
H2GISDBFactory
.
openSpatialDataBase
(
DB_NAME
))
{
try
(
Connection
con
=
dataSource
.
getConnection
(
))
{
try
(
PreparedStatement
ps
=
con
.
prepareStatement
(
"SELECT gmlid, bbox, data FROM features WHERE gmlid = ?"
))
{
ps
.
setString
(
1
,
id
.
toString
());
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment