Piyush Vishwakarma
Piyush Vishwakarma

Reputation: 1826

Trying to use two connection objects in JDBC

I have created a class ConnectionManager with method getConnection. This getConnection Method creates a new DB connection to the same DB every time it is invoked. It uses DriverManager.getConnection(). Now, I have created two different connection objects using ConnectionManager.

conn1 = ConnectionManager.getConnection()
conn2 = ConnectionManager.getConnection()

now if I do this

conn2.setAutoCommit(false);
conn1.prepareStatment(somequery);
// ....Excute a set of prepared statments using conn1
conn2.commit(); // or conn2.rollback()

Is using two different connection object, one for transaction management and other for statement execution valid? If yes, will the transaction work the same way as it would work for a single object?

Also, I am using OracleDB

Upvotes: 0

Views: 252

Answers (2)

danilonet
danilonet

Reputation: 1795

conn1 and conn2 are two different connections with two different transactions, then:

for conn2:

nothing changes because the unique command you send is a commit but nothing is changed in your data

for conn1:

if conn1 has autocommit =true , all commands sents are committed by the dmbs else nothing happen because no one commits your changes.

Upvotes: 0

Sabir Khan
Sabir Khan

Reputation: 10142

Is using two different connection object, one for transaction management and other for statement execution valid? If yes, will the transaction work the same way as it would work for a single object?

No, Its not Valid. Transaction will not work the same way as it would work for a single object.

Both are two different Connections, settings done for one Connection will not affect settings for other Connection- These two are simply two unrelated Connection Objects pointing to same database.

Upvotes: 1

Related Questions