Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

c# - How to count number of BEGIN TRANSACTION statements in PostgreSQL?

using (DbConnection dbConn = myContext.Database.GetDbConnection()) {
                        using (DbCommand dbCommand = dbConn.CreateCommand()) {
                            dbCommand.CommandText = "SELECT @@TRANCOUNT";
                            dbCommand.Transaction = transaction.GetDbTransaction();
                            return (int)dbCommand.ExecuteScalar();
                        }
                    }

The code above is used with Microsoft SQL Server to get number of BEGIN TRANSACTION statements.

What is the equivalent command for PostgreSQL? Does PostgreSQL support such method?

Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SQL Server and PG works very differently. MS SQL Server support nested transaction, so the count of BEGIN TRAN is very important to know the nest level while PostGreSQL does not support nested transaction. So there is absolutly no equivalent of @@TRANCOUNT in PostGreSQL.

Whatever how many BEGIN TRANSACTION you will execute in PostGreSQL, the first time a finalization of transaction is initiate by a COMMIT or ROLLBACK, the transaction is ended immeditaly, which is not the case in SQL Server that use the asymetric model of nested transaction (The first ROLLBACK ends the transaction by a cancel while the last COMMIT - in equivalent number of BEGIN TRAN - ends the transaction and accepts finaly all the databases modifications).

Many other functionalities are completly different between PostGreSQL and SQL Server and I am writing a series of papers about those differences. The first one is about performances of DBA queries, the secound about COUT performances and the third a complete panorama of functional differences will arrive in few days or weeks...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...