Subversion Repositories bacoAlunos

Compare Revisions

Ignore whitespace Rev 1781 → Rev 1782

/branches/v3/impl/src/java/pt/utl/ist/berserk/storage/hibernate/HibernateTransactionBroker.java
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();
}
 
/branches/v3/impl/src/java/pt/utl/ist/berserk/logic/serviceManager/TransactionalServiceInvoker.java
24,6 → 24,8
* @author Jorge Martins
* @version revision by Goncalo Luiz (gedl \AT/ rnl \DOT/ ist \DOT/ utl \DOT/ pt) at June the 15th, 2004
**/
 
 
import pt.utl.ist.berserk.BerserkConfiguration;
import pt.utl.ist.berserk.ServiceRequest;
import pt.utl.ist.berserk.ServiceResponse;
40,7 → 42,7
public class TransactionalServiceInvoker extends ServiceInvoker
{
 
static boolean debug = false;
public static boolean debug = false;
 
//TODO HIPOTESE DE TER AQUI UMA VERIFICACAO DE TRANSACAO CONTIDA
//PROBLEMA E QUE TEMOS DE USAR A THREAD SE POR ALGUMA RAZAO MUDAMOS DE TRANSACTION BROKER
50,6 → 52,7
 
public final static TransactionalServiceInvoker invocador = new TransactionalServiceInvoker();
 
 
public final Object invoke(
Object requester,
IService service,
66,6 → 69,7
FilteringResult filteringResult = null;
ITransactionBroker tb = BerserkConfiguration.getApplicationTransactionBroker();
boolean alreadyActive = tb.isTransactionActive();
 
try
{
 
119,7 → 123,11
{
//the sucessfull case
if(!alreadyActive)
{
if(debug)
System.out.println("THREAD:" + Thread.currentThread().getId() + "COMMITING transaction for service: " + service.getClass().getName() + " method:" + methodName);
tb.commitTransaction();
}
return response.getReturnObject();
}
else
243,7 → 251,11
if (passedPre)
{
if(!alreadyActive)
{
if(debug)
System.out.println("THREAD:" + Thread.currentThread().getId() + "Commiting transaction for service: " + service.getClass().getName() + " method:" + methodName);
tb.commitTransaction();
}
return true;
}
else