Skip to content

Commit 6310a4f

Browse files
committed
Remove Connection field
* Fix memory leak * Add more methods for open connections * Update dependencies * Fix create SQLite directory if plugin catch exception * Update README.md
1 parent 3185106 commit 6310a4f

4 files changed

Lines changed: 58 additions & 24 deletions

File tree

README.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Execute 1000 SELECT statements against a DB and map the data returned to a POJO.
2222
|------------------------------------------|-------------------|
2323
| Hand coded <code>ResultSet</code> | 15ms |
2424
| [Sql2o](https://github.com/aaberg/sql2o) | 24ms (60% slower) |
25-
| [JDBI](https://github.com/jdbi/jdbi) | 26ms (73% slower) |
2625

2726
## How to use plugin
2827

@@ -37,13 +36,24 @@ Here is very basic example of your MySQL database class:
3736

3837
```java
3938
import me.hteppl.data.database.MySQLDatabase;
39+
import org.sql2o.Connection;
4040

4141
public class MyDatabase extends MySQLDatabase {
4242

4343
public MyDatabase() {
4444
super("host", "database", "user", "password");
45-
// also you can execute your db scheme with
46-
// this.executeScheme("scheme");
45+
// also you can execute your db scheme with
46+
this.executeScheme("scheme");
47+
48+
// or use createConnection() method
49+
try (Connection connection = this.createConnection()) {
50+
connection.createQuery("query").executeUpdate();
51+
}
52+
53+
// if you need disable auto commit, use createTransaction() method
54+
try (Connection connection = this.createTransaction()) {
55+
connection.createQuery("query").executeUpdate();
56+
}
4757
}
4858
}
4959
```
@@ -52,28 +62,43 @@ or SQLite database class:
5262

5363
```java
5464
import me.hteppl.data.database.SQLiteDatabase;
65+
import org.sql2o.Connection;
5566

5667
public class MyDatabase extends SQLiteDatabase {
5768

5869
public MyDatabase() {
5970
super("database");
6071
// also you can execute your db scheme with
61-
// this.executeScheme("scheme");
72+
this.executeScheme("scheme");
73+
74+
// or use createConnection() method
75+
try (Connection connection = this.createConnection()) {
76+
connection.createQuery(...).executeUpdate();
77+
}
78+
79+
// if you need disable auto commit, use createTransaction() method
80+
try (Connection connection = this.createTransaction()) {
81+
connection.createQuery(...).executeUpdate();
82+
}
6283
}
6384
}
6485
```
6586

6687
After that, you can easily do what you want with your [*Sql2o*](https://www.sql2o.org) connections
6788

6889
```java
69-
import me.hteppl.data.Database;
7090
/* import your database class */
7191

7292
public class Main {
7393

7494
public static void main(String[] args) {
75-
Database db = new MyDatabase();
76-
db.getConnection().createQuery(...);
95+
MyDatabase db = new MyDatabase();
96+
try (Connection connection = db.createConnection()) {
97+
connection.createQuery(...);
98+
}
99+
100+
// or
101+
db.executeScheme("scheme");
77102
}
78103
}
79104
```

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.hteppl</groupId>
88
<artifactId>DataManager</artifactId>
9-
<version>2.0.4-SNAPSHOT</version>
9+
<version>2.0.5-SNAPSHOT</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
@@ -65,15 +65,15 @@
6565
</dependency>
6666

6767
<dependency>
68-
<groupId>mysql</groupId>
69-
<artifactId>mysql-connector-java</artifactId>
70-
<version>8.0.30</version>
68+
<groupId>com.mysql</groupId>
69+
<artifactId>mysql-connector-j</artifactId>
70+
<version>8.0.31</version>
7171
<scope>compile</scope>
7272
</dependency>
7373
<dependency>
7474
<groupId>org.xerial</groupId>
7575
<artifactId>sqlite-jdbc</artifactId>
76-
<version>3.39.3.0</version>
76+
<version>3.40.0.0</version>
7777
<scope>compile</scope>
7878
</dependency>
7979

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
package me.hteppl.data;
22

33
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
45
import org.sql2o.Connection;
56
import org.sql2o.Sql2o;
67

8+
@Getter
9+
@RequiredArgsConstructor
710
public abstract class Database {
811

9-
@Getter
10-
protected final Connection connection;
11-
@Getter
1212
private final Sql2o sql2o;
1313

14-
public Database(Sql2o sql2o) {
15-
this.sql2o = sql2o;
16-
this.connection = sql2o.open();
17-
}
18-
19-
protected void executeScheme(String scheme) {
14+
public void executeScheme(String scheme) {
2015
if (scheme != null && !scheme.isEmpty()) {
21-
this.connection.createQuery(scheme).executeUpdate();
16+
try (Connection connection = this.createConnection()) {
17+
connection.createQuery(scheme).executeUpdate();
18+
}
2219
}
2320
}
21+
22+
public Connection createConnection() {
23+
return this.sql2o.open();
24+
}
25+
26+
public Connection createTransaction() {
27+
return this.sql2o.beginTransaction();
28+
}
29+
30+
public Connection createTransaction(int isolationLevel) {
31+
return this.sql2o.beginTransaction(isolationLevel);
32+
}
2433
}

src/main/java/me/hteppl/data/database/SQLiteDatabase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public SQLiteDatabase(String folder, String database) {
2020

2121
private static Sql2o createSql2o(String folder, String database) {
2222
try {
23-
Files.createDirectories(Paths.get(folder));
24-
2523
Class.forName("org.sqlite.JDBC");
24+
25+
Files.createDirectories(Paths.get(folder));
2626
} catch (IOException | ClassNotFoundException exception) {
2727
throw new RuntimeException(exception);
2828
}

0 commit comments

Comments
 (0)