[ 1801842 ] DB connection fix & improvements for concurrent threads.

- Integrating contribution from the Posterita Team.
This commit is contained in:
Heng Sin Low 2007-09-26 09:08:59 +00:00
parent 67798db401
commit 87002bb763
8 changed files with 3681 additions and 3786 deletions

View File

@ -18,5 +18,6 @@
<classpathentry kind="lib" path="/tools/lib/postgresql.jar"/>
<classpathentry kind="lib" path="/tools/lib/ocrs12.jar"/>
<classpathentry kind="lib" path="/tools/lib/ojdbc14.jar"/>
<classpathentry kind="lib" path="/tools/lib/c3p0-0.9.1.2.jar"/>
<classpathentry kind="output" path="build"/>
</classpath>

View File

@ -275,7 +275,11 @@ public interface AdempiereDatabase
*/
// public String getDataType (int displayType, int precision,
// boolean defaultValue)
/**
* Default sql use to test whether a connection is still valid
*/
public final static String DEFAULT_CONN_TEST_SQL = "SELECT Version FROM AD_System";
} // AdempiereDatabase

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -16,9 +16,13 @@
*****************************************************************************/
package org.compiere.model;
import java.rmi.RemoteException;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.compiere.db.CConnection;
import org.compiere.interfaces.Server;
import org.compiere.util.*;
/**
@ -37,6 +41,7 @@ public class MSequence extends X_AD_Sequence
/** Log Level for Next ID Call */
private static final Level LOGLEVEL = Level.ALL;
/**
* Get next number for Key column = 0 is Error.
* @param AD_Client_ID client
@ -48,6 +53,30 @@ public class MSequence extends X_AD_Sequence
{
if (TableName == null || TableName.length() == 0)
throw new IllegalArgumentException("TableName missing");
//get from server
if (DB.isRemoteObjects())
{
Server server = CConnection.get().getServer();
try
{
if (server != null)
{ // See ServerBean
int id = server.getNextID(AD_Client_ID, TableName, trxName);
s_log.finest("server => " + id);
if (id < 0)
throw new DBException("No NextID");
return id;
}
s_log.log(Level.SEVERE, "AppsServer not found - " + TableName);
}
catch (RemoteException ex)
{
s_log.log(Level.SEVERE, "AppsServer error", ex);
}
// Try locally
}
int retValue = -1;
// Check AdempiereSys
@ -146,7 +175,8 @@ public class MSequence extends X_AD_Sequence
pstmt = null;
conn.setAutoCommit(autocommit); //jz set back
//
// conn.close();
if (trx == null && conn != null)
conn.close();
conn = null;
//
break; // EXIT
@ -241,6 +271,28 @@ public class MSequence extends X_AD_Sequence
if (TableName == null || TableName.length() == 0)
throw new IllegalArgumentException("TableName missing");
//get from server
if (DB.isRemoteObjects())
{
Server server = CConnection.get().getServer();
try
{
if (server != null)
{ // See ServerBean
String dn = server.getDocumentNo (AD_Client_ID, TableName, trxName);
s_log.finest("Server => " + dn);
if (dn != null)
return dn;
}
s_log.log(Level.SEVERE, "AppsServer not found - " + TableName);
}
catch (RemoteException ex)
{
s_log.log(Level.SEVERE, "AppsServer error", ex);
}
}
//local
// Check AdempiereSys
boolean adempiereSys = Ini.isPropertyBool(Ini.P_ADEMPIERESYS);
if (adempiereSys && AD_Client_ID > 11)
@ -344,7 +396,7 @@ public class MSequence extends X_AD_Sequence
if (trx == null)
{
conn.commit();
// conn.close();
conn.close();
}
conn = null;
}
@ -359,8 +411,8 @@ public class MSequence extends X_AD_Sequence
if (pstmt != null)
pstmt.close();
pstmt = null;
// if (conn != null && trx == null)
// conn.close();
if (trx == null && conn != null)
conn.close();
conn = null;
}
catch (Exception e)
@ -397,7 +449,30 @@ public class MSequence extends X_AD_Sequence
{
s_log.severe ("C_DocType_ID=0");
return null;
}
}
//get from server
if (DB.isRemoteObjects())
{
Server server = CConnection.get().getServer();
try
{
if (server != null)
{ // See ServerBean
String dn = server.getDocumentNo (C_DocType_ID, trxName);
s_log.finest("Server => " + dn);
if (dn != null)
return dn;
}
s_log.log(Level.SEVERE, "AppsServer not found - " + C_DocType_ID);
}
catch (RemoteException ex)
{
s_log.log(Level.SEVERE, "AppsServer error", ex);
}
}
//local
MDocType dt = MDocType.get (Env.getCtx(), C_DocType_ID); // wrong for SERVER, but r/o
if (dt != null && !dt.isDocNoControlled())
{
@ -504,7 +579,7 @@ public class MSequence extends X_AD_Sequence
if (trx == null)
{
conn.commit();
// conn.close();
conn.close();
}
conn = null;
}
@ -519,8 +594,8 @@ public class MSequence extends X_AD_Sequence
if (pstmt != null)
pstmt.close();
pstmt = null;
// if (conn != null && trx == null)
// conn.close();
if (trx == null && conn != null)
conn.close();
conn = null;
}
catch (Exception e)

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,6 @@ import java.util.logging.*;
import javax.sql.*;
import org.compiere.Adempiere;
import org.compiere.db.*;
import org.compiere.interfaces.*;
@ -30,6 +29,12 @@ import org.compiere.interfaces.*;
*
* @author Jorg Janke
* @version $Id: CStatement.java,v 1.3 2006/07/30 00:54:36 jjanke Exp $
* ---
* Modifications: Handle connections properly
* Close the associated connection when the statement is closed
* Reason : Due to changes brought in the connection pooling whereby the system
* no more relies upon abandoned connections.
* @author Ashley Ramdass (Posterita)
*/
public class CStatement implements Statement
{
@ -742,8 +747,16 @@ public class CStatement implements Statement
*/
public void close () throws SQLException
{
if (p_stmt != null)
p_stmt.close();
if (p_stmt != null)
{
Connection conn = p_stmt.getConnection();
p_stmt.close();
if (!conn.isClosed() && conn.getAutoCommit())
{
conn.close();
}
}
} // close
/*************************************************************************
@ -770,15 +783,25 @@ public class CStatement implements Statement
log.log(Level.SEVERE, p_vo.toString(), ex);
throw new RuntimeException (ex);
}
finally {
if (pstmt != null)
{
try
{
pstmt.close();
} catch (SQLException e){}
pstmt = null;
}
finally
{
if (pstmt != null)
{
try
{
Connection conn = pstmt.getConnection();
pstmt.close();
if (p_vo.getTrxName() == null && !conn.isClosed())
{
conn.close();
}
}
catch (SQLException e)
{
log.log(Level.SEVERE, e.getMessage(), e);
}
pstmt = null;
}
}
} // remote_executeUpdate
@ -811,15 +834,30 @@ public class CStatement implements Statement
catch (SQLException ex)
{
log.log(Level.SEVERE, "local", ex);
try
{
if (stmt != null)
stmt.close();
stmt = null;
}
catch (SQLException ex1)
{
}
if (stmt != null)
{
try
{
stmt.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, "Could not close statement", e);
}
stmt = null;
}
if (conn != null && p_vo.getTrxName() == null)
{
try
{
conn.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, "Could not close connection", e);
}
}
}
return stmt;
} // local_getStatement
@ -925,18 +963,7 @@ public class CStatement implements Statement
{
log.log(Level.SEVERE, p_vo.toString(), ex);
throw new RuntimeException (ex);
}
// Close Cursor
try
{
if (pstmt != null)
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, "close pstmt", e);
}
}
return rowSet;
} // local_getRowSet

File diff suppressed because it is too large Load Diff