@@ -1109,6 +1109,15 @@ impl Connection {
11091109 self . conn . borrow ( ) . is_desynchronized ( )
11101110 }
11111111
1112+ /// Determines if the `Connection` is currently "active", that is, if there
1113+ /// are no active transactions.
1114+ ///
1115+ /// The `transaction` method can only be called on the active `Connection`
1116+ /// or `Transaction`.
1117+ pub fn is_active ( & self ) -> bool {
1118+ self . conn . borrow ( ) . trans_depth == 0
1119+ }
1120+
11121121 /// Consumes the connection, closing it.
11131122 ///
11141123 /// Functionally equivalent to the `Drop` implementation for `Connection`
@@ -1234,6 +1243,11 @@ impl<'conn> Transaction<'conn> {
12341243 stmt. lazy_query ( self , params, row_limit)
12351244 }
12361245
1246+ /// Like `Connection::is_active`.
1247+ pub fn is_active ( & self ) -> bool {
1248+ self . conn . conn . borrow ( ) . trans_depth == self . depth
1249+ }
1250+
12371251 /// Determines if the transaction is currently set to commit or roll back.
12381252 pub fn will_commit ( & self ) -> bool {
12391253 self . commit . get ( )
@@ -1989,6 +2003,9 @@ pub trait GenericConnection {
19892003
19902004 /// Like `Connection::batch_execute`.
19912005 fn batch_execute ( & self , query : & str ) -> Result < ( ) > ;
2006+
2007+ /// Like `Connection::is_active`.
2008+ fn is_active ( & self ) -> bool ;
19922009}
19932010
19942011impl GenericConnection for Connection {
@@ -2016,6 +2033,10 @@ impl GenericConnection for Connection {
20162033 fn batch_execute ( & self , query : & str ) -> Result < ( ) > {
20172034 self . batch_execute ( query)
20182035 }
2036+
2037+ fn is_active ( & self ) -> bool {
2038+ self . is_active ( )
2039+ }
20192040}
20202041
20212042impl < ' a > GenericConnection for Transaction < ' a > {
@@ -2043,4 +2064,8 @@ impl<'a> GenericConnection for Transaction<'a> {
20432064 fn batch_execute ( & self , query : & str ) -> Result < ( ) > {
20442065 self . batch_execute ( query)
20452066 }
2067+
2068+ fn is_active ( & self ) -> bool {
2069+ self . is_active ( )
2070+ }
20462071}
0 commit comments