-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Apr 12, 2012, at 1:55 PM, Manlio Perillo wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hi.
> 
> I looked at SQLAlchemy source code to see examples about `with`
> statement support and found something that look suspicious to me.
> 
> class Engine:
>    ....
> 
>    def begin(self, close_with_result=False)
>        conn = self.contextual_connect(
>                  close_with_result=close_with_result)
>        trans = conn.begin()
>        return Engine._trans_ctx(conn, trans, close_with_result)
> 
> What happens in case conn.begin() fails?

the call to begin() overall would raise an exception.

> 
> I was assuming that conn.begin always executed a BEGIN SQL command, but
> this is not the case.
> However some dialects (like informix and MySQL oursql) *do* execute SQL
> commands, and these can fail.

and I guess the concern is the Connection is left to the garbage collector to 
be closed.   In rfc4e7ddc0777 I changed it to:

        conn = self.contextual_connect(close_with_result=close_with_result)
        try:
            trans = conn.begin()
        except:
            conn.close()
            raise
        return Engine._trans_ctx(conn, trans, close_with_result)

note that in other cases like engine.execute(), the "close_with_result" flag is 
unconditionally true meaning an exception there closes out the connection in 
line 1830 in _handle_dbapi_exception().

> I have another question, about coding style.
> Current SQLAlchemy code create the connection and transaction objects in
> the Engine.begin method, and then return an instance of
> Engine._trans_ctx class.
> 
> What about, instead, creating the connection and transaction objects
> inside the __enter__ method of the Engine._trans_ctx class?
> 
> Doing this way, one can prevent incorrect usage of the context manager;
> that is doing so:
> 
>   ctx = db.begin()
> 
> should not create a connection that will never be explicitly closed.

oh that's a nice idea, if you create a pull request from 
https://bitbucket.org/sqlalchemy/sqlalchemy I can pull that in, thanks !  
you'll need to adjust my new test 
test.engine.test_execute:ConvenienceExecuteTest.test_transaction_engine_ctx_begin_fails
 to use testing.run_as_contextmanager in order to hit the exception in that 
case.




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: GPGTools - http://gpgtools.org

iQEcBAEBAgAGBQJPhyLYAAoJEDMCOcHE2v7hC8gIANHeUt3534OSHQDzvxLDU4M8
lelma2zafTADVSbQm/WSE79HjTQSBgSVopWKL13lbvU44B/7yYbzVBDu4ZNb8DsX
0aM6I6W36km5EV5lPjOqeBZOAaWNquqXA8g0ic5+79K7LplUpbDfKI/Q8eSgWRjm
ZZW8pzo/cFpEN33+0U7zOh6yariFE+W5dtINVmjf3UMJbGJ1ieEyff4wflbQI+UI
q5pw4Pqby+YqBBfqC6EfAnCHKkBV61PXSbMVBQfQRVqflFxY440HYDm0zZ6WJytV
wZlISaakTKH1mPzTUT3R9flx2NsN7aU4/NkSMSkuqS3VaJhx33oj0DJNHm4ZKFE=
=1tow
-----END PGP SIGNATURE-----

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to