Home > Back-end >  Why affairs to ensure that the same database link in Java
Why affairs to ensure that the same database link in Java

Time:10-05

1. For example, A transaction right two events: (1) change the database id=1 name value in the table for A Ming (2) to delete data from the database table id=2 B
2. If the transaction in Java are need to make sure that the same database links, so we use the reduction to absurdity, both: is not the same database transaction link cannot be rolled back
3. Me, I think different database link it doesn't matter, the transaction can be rolled back


In addition: dislike huluntianzao type answer, such as: transaction, of course, to ensure that the same database link.. Etc.
Thank you all for the great god

CodePudding user response:

I understand:
Two events in the same transaction, then had the atomicity, either have occurred, or didn't happen,
Database A operation fails, the transaction rolled back, no longer to perform the operation of the database B, if A success, failure, B B database transaction rolled back, but at the moment, have completed A transaction commit, cannot be rolled back, or violated the atomicity,

CodePudding user response:

With the library affairs management is simple, as long as open a transaction, commit or rollback once at a time,
Across the library, to manage an open, multiple transactions commit, rollback, more complex,
But it doesn't mean the cross-database transaction cannot be achieved, but try to avoid across the library affairs,

CodePudding user response:

I understand the transaction
On the same data source open transaction management or successful submission, failure or rollback (bs) ok,
Actually program code in the implementation of the concept of transaction in the end will be converted into a database transaction stored procedure
So you have to make sure the same connection, such as through JDBC operating data source to ensure the same connection object
If it can't be guaranteed, that also is a violation of the firm mention atomicity, consistency, it is hard to ensure the uniformity of data (transactions) that also use a p
To operate multiple data sources application scenarios, it is recommended to use a distributed transaction to achieve unified commit or rollback
The building Lord understand the principle of a single transaction, distributed transaction, a piece of cake,

CodePudding user response:

First of all, if you use the connection pool, you to the operation of the data, using the connection pool is assigned to one of your database connection, the exception handling code, if you can get two connections, and let two connection open transactions that occur to the abnormal, after you make the two connections are rolled back, of course, no problem, but such waste connection resources, typically, a set of complete with business logic to database update in a connection, in turn, submit, when an exception occurs, the rollback the connection,

CodePudding user response:

You seem problem understanding of transaction, the Java commit/rollback transaction just call database API
As you say, multiple connections, if you don't support the xa transaction (xa transaction is very low probability of inconsistent), so it is easy to happen commit/rollback inconsistencies, such as A connection to submit successful, B connects to submit, an error occurred suddenly, this time A you cannot be rolled back, can appear not consistent, this time even database is A database is also unable to know A, B in A single transaction, for database, he is A different request, different database that is more not cicada

Suggest you go to see the database is in extreme cases, how to do data consistent, extreme is offline/down/call service timeouts, etc., and then understand the XA transaction, other data consistency of distributed situation

CodePudding user response:

Brother, if you are in a transaction to perform a table changes, then the table will be locked, this transaction if you also enable a SQL links to query the table, the table because is occupied by a affairs all the time, so you are not detected, because you haven't submitted the transaction, of course you can modify Settings a row locking operation rather than table lock operation, or you haven't modify the table but the transaction commit, others query is a query before or after your operation information

CodePudding user response:

The original poster 1 wrong oh, transaction: the so-called transaction, the matter is to complete a thing, which contains multiple tasks
When all the tasks are performed successfully, this thing is successful: all data changes take effect
When any failed to perform a task, it is failed, all changes to the data back

CodePudding user response:

See so much answer, have no a clear, must first establish a connection to operate database, after establishing a connection open session, a transaction can only be submitted in one session, a transaction can operate on multiple databases, different connection, set up the session to be sure it is not the same, so there is no one transaction contains two links,

CodePudding user response:

Database transaction is in the current database, database principle of ACID, and data isolation level are also in view of the current library, just as you want to go to A, B two door, A and B don't know you went nowhere else, also don't know what did you do in other places, and also don't interfere in, you this kind of operating multiple libraries of transaction design to the problem of distributed transactions, need other ways to solve it, such as: 2 PC, TCC, MQ methods such as transaction information

CodePudding user response:

Two libraries to ensure the atomicity that is distributed transactions, and is not to say that in the same library affairs will take effect, they must be in the same connection, if the operation is the same database, but the connection is different, the transaction also will not take effect, a distributed transaction is perfect now, such as ali's seat is very good,
  • Related