* Fixed for bug [1619933] and [1619933], now it is possible to run adempiere client with only a single connection to the application server.

This commit is contained in:
Heng Sin Low 2007-01-07 07:34:41 +00:00
parent 85be30f8c4
commit ea36c00e23
2 changed files with 174 additions and 24 deletions

View File

@ -243,6 +243,9 @@ public final class DB
* @return True if success, false otherwise * @return True if success, false otherwise
*/ */
public static boolean connect() { public static boolean connect() {
//wan profile
if (DB.isRemoteObjects()) return true;
boolean success =false; boolean success =false;
try try
{ {
@ -258,11 +261,23 @@ public final class DB
} }
/** /**
* Is there a connection to the database ? * @return true, if connected to database
* @return true, if connected to database
*/ */
public static boolean isConnected() public static boolean isConnected()
{ {
return isConnected(true);
}
/**
* Is there a connection to the database ?
* @param createNew If true, try to connect it not already connected
* @return true, if connected to database
*/
public static boolean isConnected(boolean createNew)
{
//wan profile
if (DB.isRemoteObjects()) return true;
boolean success = false; boolean success = false;
CLogErrorBuffer eb = CLogErrorBuffer.get(false); CLogErrorBuffer eb = CLogErrorBuffer.get(false);
if (eb != null && eb.isIssueError()) if (eb != null && eb.isIssueError())
@ -271,7 +286,7 @@ public final class DB
eb = null; // don't reset eb = null; // don't reset
try try
{ {
success = getConnectionRW() != null; // try to get a connection success = getConnectionRW(createNew) != null; // try to get a connection
} }
catch (Exception e) catch (Exception e)
{ {
@ -282,13 +297,25 @@ public final class DB
return success; return success;
} // isConnected } // isConnected
/**
* @return Connection (r/w)
*/
public static Connection getConnectionRW()
{
return getConnectionRW(true);
}
/** /**
* Return (pooled) r/w AutoCommit, Serializable connection. * Return (pooled) r/w AutoCommit, Serializable connection.
* For Transaction control use Trx.getConnection() * For Transaction control use Trx.getConnection()
* @param createNew If true, try to create new connection if no existing connection
* @return Connection (r/w) * @return Connection (r/w)
*/ */
public static Connection getConnectionRW () public static Connection getConnectionRW (boolean createNew)
{ {
//wan profile
if (DB.isRemoteObjects()) return null;
// check health of connection // check health of connection
try try
{ {
@ -317,10 +344,13 @@ public final class DB
// Get new // Get new
if (s_connectionRW == null) if (s_connectionRW == null)
{ {
s_connectionRW = s_cc.getConnection (true, Connection.TRANSACTION_READ_COMMITTED); if (createNew)
log.finest("Con=" + s_connectionRW); {
s_connectionRW = s_cc.getConnection (true, Connection.TRANSACTION_READ_COMMITTED);
log.finest("Con=" + s_connectionRW);
}
} }
if (s_connectionRW == null) if (s_connectionRW == null && createNew)
throw new UnsupportedOperationException("No DBConnection"); throw new UnsupportedOperationException("No DBConnection");
// //
// System.err.println ("DB.getConnectionRW - " + s_connectionRW); // System.err.println ("DB.getConnectionRW - " + s_connectionRW);
@ -335,6 +365,9 @@ public final class DB
*/ */
public static Connection getConnectionID () public static Connection getConnectionID ()
{ {
//wan profile
if (DB.isRemoteObjects()) return null;
if (s_connectionID != null) if (s_connectionID != null)
{ {
try try
@ -363,6 +396,9 @@ public final class DB
*/ */
public static Connection getConnectionRO () public static Connection getConnectionRO ()
{ {
//wan profile
if (DB.isRemoteObjects()) return null;
try try
{ {
synchronized (s_cc) // use as mutex as s_connection is null the first time synchronized (s_cc) // use as mutex as s_connection is null the first time

View File

@ -17,9 +17,14 @@
package org.compiere.util; package org.compiere.util;
import java.beans.*; import java.beans.*;
import java.rmi.RemoteException;
import java.sql.*; import java.sql.*;
import java.util.UUID;
import java.util.logging.*; import java.util.logging.*;
import org.compiere.db.CConnection;
import org.compiere.interfaces.Server;
/** /**
* Transaction Management. * Transaction Management.
* - Create new Transaction by Trx.get(name); * - Create new Transaction by Trx.get(name);
@ -32,6 +37,8 @@ import java.util.logging.*;
* @author Jorg Janke * @author Jorg Janke
* @author Low Heng Sin * @author Low Heng Sin
* - added rollback(boolean) and commit(boolean) [20070105] * - added rollback(boolean) and commit(boolean) [20070105]
* - remove unnecessary use of savepoint
* - use UUID for safer transaction name generation
* @version $Id$ * @version $Id$
*/ */
public class Trx implements VetoableChangeListener public class Trx implements VetoableChangeListener
@ -74,7 +81,7 @@ public class Trx implements VetoableChangeListener
{ {
if (prefix == null || prefix.length() == 0) if (prefix == null || prefix.length() == 0)
prefix = "Trx"; prefix = "Trx";
prefix += "_" + System.currentTimeMillis(); prefix += "_" + UUID.randomUUID(); //System.currentTimeMillis();
return prefix; return prefix;
} // createTrxName } // createTrxName
@ -114,7 +121,7 @@ public class Trx implements VetoableChangeListener
private Connection m_connection = null; private Connection m_connection = null;
private String m_trxName = null; private String m_trxName = null;
private Savepoint m_savepoint = null; //private Savepoint m_savepoint = null;
private boolean m_active = false; private boolean m_active = false;
/** /**
@ -124,6 +131,9 @@ public class Trx implements VetoableChangeListener
public Connection getConnection() public Connection getConnection()
{ {
log.log(Level.ALL, "Active=" + isActive() + ", Connection=" + m_connection); log.log(Level.ALL, "Active=" + isActive() + ", Connection=" + m_connection);
//wan profile
if (DB.isRemoteObjects()) return null;
if (m_connection == null) // get new Connection if (m_connection == null) // get new Connection
setConnection(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED)); setConnection(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
if (!isActive()) if (!isActive())
@ -179,26 +189,27 @@ public class Trx implements VetoableChangeListener
*/ */
public boolean start() public boolean start()
{ {
if (m_savepoint != null || m_active) if (/*m_savepoint != null || */m_active)
{ {
log.warning("Trx in progress " + m_trxName + " - " + m_savepoint); log.warning("Trx in progress " + m_trxName /*+ " - " + m_savepoint*/);
return false; return false;
} }
m_active = true; m_active = true;
/*
try try
{ {
if (m_connection != null) if (m_connection != null)
{ {
m_savepoint = m_connection.setSavepoint(m_trxName); //m_savepoint = m_connection.setSavepoint(m_trxName);
log.info("**** " + getTrxName()); log.info("**** " + getTrxName());
} }
} }
catch (SQLException e) catch (SQLException e)
{ {
log.log(Level.SEVERE, m_trxName, e); log.log(Level.SEVERE, m_trxName, e);
m_savepoint = null; //m_savepoint = null;
return false; return false;
} }*/
return true; return true;
} // startTrx } // startTrx
@ -206,10 +217,11 @@ public class Trx implements VetoableChangeListener
* Get Savepoint * Get Savepoint
* @return savepoint or null * @return savepoint or null
*/ */
/*
public Savepoint getSavepoint() public Savepoint getSavepoint()
{ {
return m_savepoint; return m_savepoint;
} // getSavepoint } // getSavepoint*/
/** /**
* Transaction is Active * Transaction is Active
@ -227,16 +239,25 @@ public class Trx implements VetoableChangeListener
*/ */
public boolean rollback(boolean throwException) throws SQLException public boolean rollback(boolean throwException) throws SQLException
{ {
//remote
if (DB.isRemoteObjects())
{
return remote_rollback(throwException);
}
//local
try try
{ {
if (m_connection != null) if (m_connection != null)
{ {
/*
if (m_savepoint == null) if (m_savepoint == null)
m_connection.rollback(); m_connection.rollback();
else else
m_connection.rollback(m_savepoint); m_connection.rollback(m_savepoint);*/
m_connection.rollback();
log.info ("**** " + m_trxName); log.info ("**** " + m_trxName);
m_savepoint = null; //m_savepoint = null;
m_active = false; m_active = false;
return true; return true;
} }
@ -246,12 +267,12 @@ public class Trx implements VetoableChangeListener
log.log(Level.SEVERE, m_trxName, e); log.log(Level.SEVERE, m_trxName, e);
if (throwException) if (throwException)
{ {
m_savepoint = null; //m_savepoint = null;
m_active = false; m_active = false;
throw e; throw e;
} }
} }
m_savepoint = null; //m_savepoint = null;
m_active = false; m_active = false;
return false; return false;
} // rollback } // rollback
@ -269,6 +290,49 @@ public class Trx implements VetoableChangeListener
} }
} }
/**
* Rollback a remote transaction
* @param throwException
* @return true if success, false otherwise
* @throws SQLException
*/
private boolean remote_rollback(boolean throwException) throws SQLException
{
Server server = CConnection.get().getServer();
try
{
if (server != null)
{ // See ServerBean
return server.rollback(m_trxName);
}
log.log(Level.SEVERE, "AppsServer not found");
if (throwException)
throw new SQLException("AppsServer not found");
return false;
}
catch (RemoteException ex)
{
log.log(Level.SEVERE, "AppsServer error", ex);
if (throwException)
{
if (ex.getCause() instanceof RuntimeException)
{
RuntimeException r = (RuntimeException)ex.getCause();
if (r.getCause() instanceof SQLException)
throw (SQLException)r.getCause();
else if ( r.getCause() != null )
throw new SQLException("Application server exception - " + r.getCause().getMessage());
else
throw new SQLException("Application server exception - " + r.getMessage());
}
else
throw new SQLException("Application server exception - " + ex.getMessage());
}
return false;
}
}
/** /**
* Release savepoint * Release savepoint
* @return true if released * @return true if released
@ -302,13 +366,20 @@ public class Trx implements VetoableChangeListener
**/ **/
public boolean commit(boolean throwException) throws SQLException public boolean commit(boolean throwException) throws SQLException
{ {
//remote
if (DB.isRemoteObjects())
{
return remote_commit(throwException);
}
//local
try try
{ {
if (m_connection != null) if (m_connection != null)
{ {
m_connection.commit(); m_connection.commit();
log.info ("**** " + m_trxName); log.info ("**** " + m_trxName);
m_savepoint = null; //m_savepoint = null;
m_active = false; m_active = false;
return true; return true;
} }
@ -318,16 +389,59 @@ public class Trx implements VetoableChangeListener
log.log(Level.SEVERE, m_trxName, e); log.log(Level.SEVERE, m_trxName, e);
if (throwException) if (throwException)
{ {
m_savepoint = null; //m_savepoint = null;
m_active = false; m_active = false;
throw e; throw e;
} }
} }
m_savepoint = null; //m_savepoint = null;
m_active = false; m_active = false;
return false; return false;
} // commit } // commit
/**
* Commit a remote transaction
* @param throwException
* @return true if success, false otherwise
* @throws SQLException
*/
private boolean remote_commit(boolean throwException) throws SQLException
{
Server server = CConnection.get().getServer();
try
{
if (server != null)
{ // See ServerBean
return server.commit(m_trxName);
}
log.log(Level.SEVERE, "AppsServer not found");
if (throwException)
throw new SQLException("AppsServer not found");
return false;
}
catch (RemoteException ex)
{
log.log(Level.SEVERE, "AppsServer error", ex);
if (throwException)
{
if (ex.getCause() instanceof RuntimeException)
{
RuntimeException r = (RuntimeException)ex.getCause();
if (r.getCause() instanceof SQLException)
throw (SQLException)r.getCause();
else if ( r.getCause() != null )
throw new SQLException("Application server exception - " + r.getCause().getMessage());
else
throw new SQLException("Application server exception - " + r.getMessage());
}
else
throw new SQLException("Application server exception - " + ex.getMessage());
}
return false;
}
}
/** /**
* Commit * Commit
* @return true if success * @return true if success
@ -357,7 +471,7 @@ public class Trx implements VetoableChangeListener
if (m_connection == null) if (m_connection == null)
return true; return true;
if (m_savepoint != null || isActive()) if (/*m_savepoint != null || */isActive())
commit(); commit();
// Close Connection // Close Connection
@ -369,7 +483,7 @@ public class Trx implements VetoableChangeListener
{ {
log.log(Level.SEVERE, m_trxName, e); log.log(Level.SEVERE, m_trxName, e);
} }
m_savepoint = null; //m_savepoint = null;
m_connection = null; m_connection = null;
m_active = false; m_active = false;
log.config(m_trxName); log.config(m_trxName);