Skip to content

Commit

Permalink
Remove Connection field
Browse files Browse the repository at this point in the history
* Fix memory leak
* Add more methods for open connections
* Update dependencies
* Fix create SQLite directory if plugin catch exception
* Update README.md
  • Loading branch information
IWareQ committed Jan 13, 2023
1 parent 3185106 commit 6310a4f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Execute 1000 SELECT statements against a DB and map the data returned to a POJO.
|------------------------------------------|-------------------|
| Hand coded <code>ResultSet</code> | 15ms |
| [Sql2o](https://github.com/aaberg/sql2o) | 24ms (60% slower) |
| [JDBI](https://github.com/jdbi/jdbi) | 26ms (73% slower) |

## How to use plugin

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

```java
import me.hteppl.data.database.MySQLDatabase;
import org.sql2o.Connection;

public class MyDatabase extends MySQLDatabase {

public MyDatabase() {
super("host", "database", "user", "password");
// also you can execute your db scheme with
// this.executeScheme("scheme");
// also you can execute your db scheme with
this.executeScheme("scheme");

// or use createConnection() method
try (Connection connection = this.createConnection()) {
connection.createQuery("query").executeUpdate();
}

// if you need disable auto commit, use createTransaction() method
try (Connection connection = this.createTransaction()) {
connection.createQuery("query").executeUpdate();
}
}
}
```
Expand All @@ -52,28 +62,43 @@ or SQLite database class:

```java
import me.hteppl.data.database.SQLiteDatabase;
import org.sql2o.Connection;

public class MyDatabase extends SQLiteDatabase {

public MyDatabase() {
super("database");
// also you can execute your db scheme with
// this.executeScheme("scheme");
this.executeScheme("scheme");

// or use createConnection() method
try (Connection connection = this.createConnection()) {
connection.createQuery(...).executeUpdate();
}

// if you need disable auto commit, use createTransaction() method
try (Connection connection = this.createTransaction()) {
connection.createQuery(...).executeUpdate();
}
}
}
```

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

```java
import me.hteppl.data.Database;
/* import your database class */

public class Main {

public static void main(String[] args) {
Database db = new MyDatabase();
db.getConnection().createQuery(...);
MyDatabase db = new MyDatabase();
try (Connection connection = db.createConnection()) {
connection.createQuery(...);
}

// or
db.executeScheme("scheme");
}
}
```
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.hteppl</groupId>
<artifactId>DataManager</artifactId>
<version>2.0.4-SNAPSHOT</version>
<version>2.0.5-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down Expand Up @@ -65,15 +65,15 @@
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.39.3.0</version>
<version>3.40.0.0</version>
<scope>compile</scope>
</dependency>

Expand Down
29 changes: 19 additions & 10 deletions src/main/java/me/hteppl/data/Database.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package me.hteppl.data;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.sql2o.Connection;
import org.sql2o.Sql2o;

@Getter
@RequiredArgsConstructor
public abstract class Database {

@Getter
protected final Connection connection;
@Getter
private final Sql2o sql2o;

public Database(Sql2o sql2o) {
this.sql2o = sql2o;
this.connection = sql2o.open();
}

protected void executeScheme(String scheme) {
public void executeScheme(String scheme) {
if (scheme != null && !scheme.isEmpty()) {
this.connection.createQuery(scheme).executeUpdate();
try (Connection connection = this.createConnection()) {
connection.createQuery(scheme).executeUpdate();
}
}
}

public Connection createConnection() {
return this.sql2o.open();
}

public Connection createTransaction() {
return this.sql2o.beginTransaction();
}

public Connection createTransaction(int isolationLevel) {
return this.sql2o.beginTransaction(isolationLevel);
}
}
4 changes: 2 additions & 2 deletions src/main/java/me/hteppl/data/database/SQLiteDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public SQLiteDatabase(String folder, String database) {

private static Sql2o createSql2o(String folder, String database) {
try {
Files.createDirectories(Paths.get(folder));

Class.forName("org.sqlite.JDBC");

Files.createDirectories(Paths.get(folder));
} catch (IOException | ClassNotFoundException exception) {
throw new RuntimeException(exception);
}
Expand Down

0 comments on commit 6310a4f

Please sign in to comment.