On Sun, Mar 30, 2014 at 4:42 PM, Marko Tiikkaja <
ma...@joh.to> wrote:
> On 2014-03-30 4:16 PM, Jan Mercl wrote:
>>
>> It seems the current model forces the driver to choose from
>> transaction isolation or transaction nesting support - depending on if
>> the transaction context is attached to driver.Conn (the intended case
>> I think) or to a specific DB per se.
>
> I have no idea what you mean by this. Transaction isolation and nesting are
> two completely different concepts, and I don't see how they're related.
Cf. "depending on if the transaction context is attached to
driver.Conn (the intended case I think) or to a specific DB per se",
ie. this is only about how the sql and sql/driver model works, not
about the general situation.
>> Transactions can stay isolated and have (optional) nesting support by,
>> for example:
>>
>> <description of a new interface>
>
>
> How is this better than just running txn.Exec("BEGIN") etc. manually?
""""
If the database has a concept of per-connection state, such state can
only be reliably observed within a transaction.
""""[0]
If 'transaction' means [1] then it is IMO clear that clients of
database/sql should not manage their own transactions.
""""
Prepare creates a prepared statement for use within a transaction.
""""[2]
If a client creates her own nested transaction and calls tx.Prepare,
the above will probably will not be true.
""""
Stmt returns a transaction-specific prepared statement from an
existing statement.
""""[3]
Ditto.
""""
You should not mingle the use of transaction-related functions such as
Begin() and Commit() with SQL statements such as BEGIN and COMMIT in
your SQL code. Bad things might result:
- The Tx objects could remain open, reserving a connection from the
pool and not returning it.
- The state of the database could get out of sync with the state of
the Go variables representing it.
- You could believe you’re executing queries on a single connection,
inside of a transaction, when in reality Go has created several
connections for you invisibly and some statements aren’t part of the
transaction.
""""[4]
The above seems to indicate that the rule of thumb to not use own
transactions with database/sql is recognized by some others as well.
[0]:
http://golang.org/pkg/database/sql/#DB
[1]:
http://golang.org/pkg/database/sql/#Tx
[2]:
http://golang.org/pkg/database/sql/#Tx.Prepare
[3]:
http://golang.org/pkg/database/sql/#Tx.Stmt
[4]:
http://go-database-sql.org/modifying.html#working_with_transactions
-j