Date: Thursday, December 15, 2005 @ 19:00:50
Author: gilles
Path: /cvsroot/carob/carob/src
Modified: Connection.cpp (1.41 -> 1.42)
Made use of ScopeLock new utility class to enlight code: just declared a local
variable of type ScopeLock... the unlocking is done automatically
----------------+
Connection.cpp | 163 +++++++++++++------------------------------------------
1 files changed, 41 insertions(+), 122 deletions(-)
Index: carob/src/Connection.cpp
diff -u carob/src/Connection.cpp:1.41 carob/src/Connection.cpp:1.42
--- carob/src/Connection.cpp:1.41 Tue Dec 13 18:48:31 2005
+++ carob/src/Connection.cpp Thu Dec 15 19:00:50 2005
@@ -217,18 +217,17 @@
{
wstring fctName(L"Connection::closeConnection");
+ //Wait until other methods/Commands are done
+ LockScope ls(&connectionCS);
+
if (driverSocketPtr == NULL)
{
return false;
}
-
- //Wait until other methods/Commands are done
- connectionCS.Enter();
if (connectionPooling)
{
//TODO: real connection pooling...
- connectionCS.Leave();
throw NotImplementedException(L"Connection pooling not implemented yet");
try
{
@@ -265,9 +264,6 @@
//now close the socket itself
closeOK &= driverSocketPtr->closeSocket();
- //Release lock
- connectionCS.Leave();
-
//TODO: Add connection to the pool
return closeOK;
@@ -281,9 +277,6 @@
+ sioe.description() + L")");
}
- //Release lock
- connectionCS.Leave();
-
//TODO: Do we have to throw the exception again ? reconnect ?
//good question, thank you. It is not mandatory to send an exception
//because we are at closing time... But for connection pooling purposes ?
@@ -297,13 +290,10 @@
{
logError(fctName, L"Exception while closing connection");
}
- //Release lock
- connectionCS.Leave();
+ //Don't re-throw = transparent closing
return false;
}
}
- //Release lock
- connectionCS.Leave();
return false;
}
@@ -313,7 +303,7 @@
{
wstring fctName(L"Connection::setAutoCommit");
- connectionCS.Enter();
+ LockScope ls(&connectionCS);
if (mustBeginTransaction) // so this.autoCommit is false
{
@@ -321,7 +311,6 @@
{ // Just cancel the need to start a transaction
autoCommit = true;
mustBeginTransaction = false;
- connectionCS.Leave();
return;
}
}
@@ -329,7 +318,6 @@
// nothing to change
if (autoCommit == autoCommitPrm)
{
- connectionCS.Leave();
return;
}
// autocommit false -> true
@@ -344,7 +332,6 @@
writeExecutedInTransaction = false;
transactionId = 0;
autoCommit = true;
- connectionCS.Leave();
return;
}
else // autocommit true -> false
@@ -354,7 +341,6 @@
// send the BEGIN right now (unlike pgjdbc & others)
beginTransactionIfNeeded();
-
autoCommit = false;
@@ -362,8 +348,6 @@
logDebug(fctName, L"Transaction" + toWString(transactionId)
+ L" has been started");
}
-
- connectionCS.Leave();
}
void Connection::beginTransactionIfNeeded() throw (SocketIOException,
@@ -392,19 +376,11 @@
{
wstring fctName(L"Connection::commit");
- connectionCS.Enter();
- try
- {
- checkIfConnected();
- }
- catch (...)
- {
- connectionCS.Leave();
- throw;
- }
+ LockScope ls(&connectionCS);
+ checkIfConnected();
+
if (autoCommit)
{
- connectionCS.Leave();
throw DriverException(L"Trying to commit a connection in autocommit mode");
}
try
@@ -421,7 +397,6 @@
}
// Commit must be followed by a BEGIN
mustBeginTransaction = true;
- connectionCS.Leave();
}
catch (SocketIOException e)
{
@@ -430,12 +405,6 @@
logWarning(fctName, L"I/O Error occured around commit of transaction '"
+ toWString(transactionId) + L"\n" + e.description());
}
- connectionCS.Leave();
- throw;
- }
- catch (...)
- {
- connectionCS.Leave();
throw;
}
}
@@ -445,20 +414,12 @@
{
wstring fctName(L"Connection::rollback");
- connectionCS.Enter();
+ LockScope ls(&connectionCS);
+
+ checkIfConnected();
- try
- {
- checkIfConnected();
- }
- catch (...)
- {
- connectionCS.Leave();
- throw;
- }
if (autoCommit)
{
- connectionCS.Leave();
throw DriverException(L"Trying to rollback a connection in autocommit
mode");
}
@@ -476,7 +437,6 @@
}
// Rollback is followed by a BEGIN
mustBeginTransaction = true;
- connectionCS.Leave();
}
catch (SocketIOException e)
{
@@ -485,12 +445,6 @@
logWarning(fctName, L"I/O Error occured around rollback of transaction '"
+ toWString(transactionId) + L"\n" + e.description());
}
- connectionCS.Leave();
- throw;
- }
- catch (...)
- {
- connectionCS.Leave();
throw;
}
}
@@ -525,7 +479,7 @@
wstring fctName(L"Connection::statementExecuteQuery");
- connectionCS.Enter();
+ LockScope ls(&connectionCS);
DriverResultSet* retVal = NULL;
@@ -555,16 +509,10 @@
}
catch (SocketIOException sioe)
{
- connectionCS.Leave();
//TODO: Reconnect and retry
throw;
}
- catch (...)
- {
- connectionCS.Leave();
- throw;
- }
- connectionCS.Leave();
+
return retVal;
}
@@ -574,7 +522,7 @@
{
wstring fctName(L"Connection::statementExecuteUpdate");
- connectionCS.Enter();
+ LockScope ls(&connectionCS);
int controllerResponse = -1;
@@ -610,17 +558,11 @@
// At this point the query failed before any controller succeeded in
// executing the query
- connectionCS.Leave();
throw SocketIOException(msg);
//in case exception is not catched
return -1;
}
- catch (...)
- {
- connectionCS.Leave();
- throw;
- }
- connectionCS.Leave();
+
return controllerResponse;
}
@@ -631,7 +573,8 @@
{
wstring fctName(L"Connection::statementExecute");
- connectionCS.Enter();
+ LockScope ls(&connectionCS);
+
std::list<ResultSetOrUpdateCount> results;
bool requestIdIsSet = false;
@@ -671,17 +614,11 @@
//
// return statementExecute(request);
- connectionCS.Leave();
throw SocketIOException(msg);
//in case exception is not catched
return results;
}
- catch (...)
- {
- connectionCS.Leave();
- throw;
- }
- connectionCS.Leave();
+
return results;
}
@@ -700,36 +637,26 @@
{
wstring fctName(L"Connection::tryFetchNext");
- connectionCS.Enter();
+ LockScope ls(&connectionCS);
- try
+ checkIfConnected();
+ sendCommand(*driverSocketPtr, FetchNextResultSetRows);
+ *driverSocketPtr<<cursorName;
+ *driverSocketPtr<<fetchSize;
+ if (isVerboseEnabled())
{
- checkIfConnected();
- sendCommand(*driverSocketPtr, FetchNextResultSetRows);
- *driverSocketPtr<<cursorName;
- *driverSocketPtr<<fetchSize;
- if (isVerboseEnabled())
- {
- logVerbose(fctName, L"Fetching next " + toWString(fetchSize) + L" from "
- + cursorName);
- }
- TypeTag tag(*driverSocketPtr);
-
- if (tag == TT_EXCEPTION)
- receiveException();
+ logVerbose(fctName, L"Fetching next " + toWString(fetchSize) + L" from "
+ + cursorName);
+ }
+ TypeTag tag(*driverSocketPtr);
- if (tag != TT_NOT_EXCEPTION)
- throw ProtocolException(L"Protocol exception while trying fetchNext");
+ if (tag == TT_EXCEPTION)
+ receiveException();
- // success, now we can let the DriverResultSet caller receive its data.
+ if (tag != TT_NOT_EXCEPTION)
+ throw ProtocolException(L"Protocol exception while trying fetchNext");
- }
- catch (...)
- {
- connectionCS.Leave();
- throw;
- }
- connectionCS.Leave();
+ // success, now we can let the DriverResultSet caller receive its data.
}
@@ -739,23 +666,15 @@
{
wstring fctName(L"Connection::closeRemoteResultSet");
- connectionCS.Enter();
- try
- {
- checkIfConnected();
- sendCommand(*driverSocketPtr, CloseRemoteResultSet);
- *driverSocketPtr<<cursorName;
- if (isDebugEnabled())
- logDebug(fctName, L"Closing remote ResultSet");
+ LockScope ls(&connectionCS);
- receiveBoolOrException();
- }
- catch (...)
- {
- connectionCS.Leave();
- throw;
- }
- connectionCS.Leave();
+ checkIfConnected();
+ sendCommand(*driverSocketPtr, CloseRemoteResultSet);
+ *driverSocketPtr<<cursorName;
+ if (isDebugEnabled())
+ logDebug(fctName, L"Closing remote ResultSet");
+
+ receiveBoolOrException();
}
bool Connection::receiveBoolOrException() throw (SocketIOException,
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits