Skip to content

Commit

Permalink
added DB.exec and DB.execWithTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Jul 20, 2015
1 parent b5177f6 commit a5a60d5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
20 changes: 18 additions & 2 deletions modules/dbsupport/src/main/java/org/jpos/ee/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jpos.util.LogEvent;
import org.jpos.util.Logger;

import java.io.Closeable;
import java.util.Set;

/**
Expand All @@ -41,8 +42,7 @@
* to Hibernate O/R mapping engine
*/
@SuppressWarnings({"UnusedDeclaration"})
public class DB
{
public class DB implements Closeable {
Session session;
Log log;

Expand Down Expand Up @@ -229,6 +229,22 @@ public synchronized void setLog(Log log)
this.log = log;
}

public static Object exec (DBAction action) {
try (DB db = new DB()) {
db.open();
return action.exec(db);
}
}
public static Object execWithTransaction (DBAction action) {
try (DB db = new DB()) {
db.open();
db.beginTransaction();
Object obj = action.exec(db);
db.commit();
return obj;
}
}

@SuppressWarnings({"unchecked"})
public void printStats()
{
Expand Down
24 changes: 24 additions & 0 deletions modules/dbsupport/src/main/java/org/jpos/ee/DBAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2015 Alejandro P. Revilla
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.jpos.ee;

@FunctionalInterface
public interface DBAction {
Object exec (DB db);
}
6 changes: 6 additions & 0 deletions modules/testbed/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ description = 'jPOS-EE :: Testbed'

dependencies {
compile project(':modules:core')
compile project(':modules:logback')
compile project(':modules:dbsupport')
compile project(':modules:db-h2')
compile project(':modules:sysconfig')
compile project(':modules:eeuser')
compile project(':modules:syslog')
}

apply from: '../../jpos-app.gradle'
Expand Down

0 comments on commit a5a60d5

Please sign in to comment.