@@ -1026,10 +1026,14 @@ SQLRETURN OdbcConnection::sqlNativeSql( SQLCHAR * inStatementText, SQLINTEGER te
10261026 outText = (const char *)tempNative;
10271027
10281028 }
1029- catch ( std::exception &ex )
1029+ catch (const SQLException &ex)
10301030 {
1031- SQLException &exception = (SQLException&)ex;
1032- postError ( " HY000" , exception );
1031+ postError (" HY000" , ex);
1032+ return SQL_ERROR ;
1033+ }
1034+ catch (const std::exception &ex)
1035+ {
1036+ postError (" HY000" , ex);
10331037 return SQL_ERROR ;
10341038 }
10351039
@@ -1125,20 +1129,28 @@ SQLRETURN OdbcConnection::sqlDisconnect()
11251129 if (connection->getTransactionPending ())
11261130 return sqlReturn (SQL_ERROR , " 25000" , " Invalid transaction state" );
11271131
1132+ auto handle_error = [&](const char * state, auto & ex) -> SQLRETURN
1133+ {
1134+ postError (" 01002" , ex);
1135+ connection = NULL ;
1136+ connected = false ;
1137+ return SQL_SUCCESS_WITH_INFO ;
1138+ };
1139+
11281140 try
11291141 {
11301142 connection->commit ();
11311143 releaseObjects ();
11321144 connection = NULL ;
11331145 connected = false ;
11341146 }
1135- catch ( std::exception &ex )
1147+ catch (const SQLException &ex)
11361148 {
1137- SQLException &exception = (SQLException&)ex ;
1138- postError ( " 01002 " , exception);
1139- connection = NULL ;
1140- connected = false ;
1141- return SQL_SUCCESS_WITH_INFO ;
1149+ return handle_error ( " 01002 " , ex) ;
1150+ }
1151+ catch ( const std::exception &ex)
1152+ {
1153+ return handle_error ( " 01002 " , ex) ;
11421154 }
11431155
11441156 return sqlSuccess ();
@@ -1632,6 +1644,21 @@ SQLRETURN OdbcConnection::connect(const char *sharedLibrary, const char * databa
16321644{
16331645 Properties *properties = NULL ;
16341646
1647+ // Shared rollback for both catch handlers below. `connection` may be
1648+ // NULL if createConnection() itself threw before assignment, so it has
1649+ // to be checked before close().
1650+ auto rollbackPartialConnect = [&]() {
1651+ if ( env->envShare )
1652+ env->envShare = NULL ;
1653+ if ( properties )
1654+ properties->release ();
1655+ if ( connection )
1656+ {
1657+ connection->close ();
1658+ connection = NULL ;
1659+ }
1660+ };
1661+
16351662 try
16361663 {
16371664 connection = createConnection ();
@@ -1709,19 +1736,15 @@ SQLRETURN OdbcConnection::connect(const char *sharedLibrary, const char * databa
17091736 WcsToMbs = connection->getConnectionWcsToMbs ();
17101737 MbsToWcs = connection->getConnectionMbsToWcs ();
17111738 }
1712- catch ( std::exception &ex )
1739+ catch (const SQLException &ex)
17131740 {
1714- SQLException &exception = (SQLException&)ex;
1715- if ( env->envShare )
1716- env->envShare = NULL ;
1717-
1718- if ( properties )
1719- properties->release ();
1720-
1721- connection->close ();
1722- connection = NULL ;
1723-
1724- return sqlReturn ( SQL_ERROR , " 08004" , exception.getText (), exception.getSqlcode () );
1741+ rollbackPartialConnect ();
1742+ return sqlReturn ( SQL_ERROR , " 08004" , ex.getText (), ex.getSqlcode () );
1743+ }
1744+ catch (const std::exception &ex)
1745+ {
1746+ rollbackPartialConnect ();
1747+ return sqlReturn ( SQL_ERROR , " 08004" , ex.what (), 0 );
17251748 }
17261749
17271750 connected = true ;
@@ -1746,10 +1769,14 @@ SQLRETURN OdbcConnection::sqlEndTran(int operation)
17461769 connection->rollbackAuto ();
17471770 }
17481771 }
1749- catch ( std::exception &ex )
1772+ catch (const SQLException &ex)
17501773 {
1751- SQLException &exception = (SQLException&)ex;
1752- postError (" S1000" , exception);
1774+ postError (" S1000" , ex);
1775+ return SQL_ERROR ;
1776+ }
1777+ catch (const std::exception &ex)
1778+ {
1779+ postError (" S1000" , ex);
17531780 return SQL_ERROR ;
17541781 }
17551782
@@ -1764,10 +1791,14 @@ SQLRETURN OdbcConnection::sqlExecuteCreateDatabase(const char * sqlString)
17641791 {
17651792 connection->sqlExecuteCreateDatabase ( sqlString );
17661793 }
1767- catch ( std::exception &ex )
1794+ catch (const SQLException &ex)
1795+ {
1796+ postError ( " HY000" , ex );
1797+ return SQL_ERROR ;
1798+ }
1799+ catch (const std::exception &ex)
17681800 {
1769- SQLException &exception = (SQLException&)ex;
1770- postError ( " HY000" , exception );
1801+ postError ( " HY000" , ex );
17711802 return SQL_ERROR ;
17721803 }
17731804
@@ -2178,10 +2209,13 @@ void OdbcConnection::initUserEvents( PODBC_EVENTS_BLOCK_INFO infoEvents )
21782209 userEventsInterfase->events = infoEvents->events ;
21792210 userEventsInterfase->count = infoEvents->count ;
21802211 }
2181- catch ( std::exception &ex )
2212+ catch (const SQLException &ex)
21822213 {
2183- SQLException &exception = (SQLException&)ex;
2184- postError ( " HY000" , exception );
2214+ postError ( " HY000" , ex );
2215+ }
2216+ catch (const std::exception &ex)
2217+ {
2218+ postError ( " HY000" , ex );
21852219 }
21862220}
21872221
@@ -2199,10 +2233,13 @@ void OdbcConnection::updateResultEvents( char *updated )
21992233 nextNameEvent->changed = userEvents->isChanged ( i );
22002234 }
22012235 }
2202- catch ( std::exception &ex )
2236+ catch (const SQLException &ex)
2237+ {
2238+ postError ( " HY000" , ex );
2239+ }
2240+ catch (const std::exception &ex)
22032241 {
2204- SQLException &exception = (SQLException&)ex;
2205- postError ( " HY000" , exception );
2242+ postError ( " HY000" , ex );
22062243 }
22072244}
22082245
@@ -2212,10 +2249,13 @@ void OdbcConnection::requeueEvents()
22122249 {
22132250 userEvents->queEvents ( userEventsInterfase );
22142251 }
2215- catch ( std::exception &ex )
2252+ catch (const SQLException &ex)
2253+ {
2254+ postError ( " HY000" , ex );
2255+ }
2256+ catch (const std::exception &ex)
22162257 {
2217- SQLException &exception = (SQLException&)ex;
2218- postError ( " HY000" , exception );
2258+ postError ( " HY000" , ex );
22192259 }
22202260}
22212261
0 commit comments