4,10 → 4,13 |
import org.hibernate.LockMode; |
import org.hibernate.Session; |
import org.hibernate.Transaction; |
import pt.utl.ist.berserk.logic.serviceManager.TransactionalServiceInvoker; |
import pt.utl.ist.berserk.storage.ITransactionBroker; |
import pt.utl.ist.berserk.storage.exceptions.StorageException; |
|
import java.util.HashMap; |
import java.util.List; |
import java.util.Map; |
|
/** |
* Creates an hibernate transaction |
22,6 → 25,7 |
{ |
private static HibernateTransactionBroker instance; |
|
|
private HibernateTransactionBroker() |
{ |
} |
34,28 → 38,50 |
} |
|
|
Map<Long,String> openedTransactions = new HashMap<Long,String>(); |
|
@Override |
public boolean isTransactionActive() throws StorageException { |
return HibernateUtils.getCurrentSession().getTransaction().isActive(); |
public boolean isTransactionActive() throws StorageException |
{ |
|
boolean opened = openedTransactions.containsKey(Thread.currentThread().getId()); |
|
if(TransactionalServiceInvoker.debug) |
{ |
boolean wasCommited = HibernateUtils.getCurrentSession().getTransaction().wasCommitted(); |
boolean wasRoledBack = HibernateUtils.getCurrentSession().getTransaction().wasRolledBack(); |
boolean isActive = HibernateUtils.getCurrentSession().getTransaction().isActive(); |
System.out.println("THREAD:" + Thread.currentThread().getId() + ": Was Commited: " + wasCommited); |
System.out.println("THREAD:" + Thread.currentThread().getId() + ": Was RolledBack: " + wasRoledBack); |
System.out.println("THREAD:" + Thread.currentThread().getId() + ": Is Active: " + isActive); |
} |
//if(!isActive) |
// return false; |
|
//return !wasCommited && !wasRoledBack; |
return opened; |
} |
|
public void beginTransaction() throws StorageException |
{ |
if(HibernateUtils.getCurrentSession().getTransaction().isActive()) |
{ |
System.out.println(">>>>>>TRANSACTION PROBLEM: AVISO: Já existia uma transação activa e foi iniciada outra neste momento sem fechar a anterior "); |
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { |
System.out.println("\t" + ste); |
} |
} |
boolean opened = openedTransactions.containsKey(Thread.currentThread().getId()); |
if(!opened) |
openedTransactions.put(Thread.currentThread().getId(),"beginTransaction"); |
HibernateUtils.getCurrentSession().beginTransaction(); |
} |
|
public void commitTransaction() throws StorageException |
{ |
|
|
|
HibernateUtils.getCurrentSession().getTransaction().commit(); |
// HibernateUtils.getCurrentSession().close(); |
|
boolean opened = openedTransactions.containsKey(Thread.currentThread().getId()); |
if(opened) |
openedTransactions.remove(Thread.currentThread().getId()); |
|
} |
|
public void abortTransaction() throws StorageException |
63,6 → 89,10 |
Session sess = HibernateUtils.getCurrentSession(); |
Transaction t = sess.getTransaction(); |
t.rollback(); |
|
boolean opened = openedTransactions.containsKey(Thread.currentThread().getId()); |
if(opened) |
openedTransactions.remove(Thread.currentThread().getId()); |
// sess.close(); |
} |
|