Hướng dẫn sử dụng github Informational, Transactional năm 2024
This at least is a common enough idea, though far from being shared by everyone. Just ask @gavinking . Show
But regardless, without arguing with this idea: people advocating for this generally also advocate for an intermediate layer between your rest layer and your repository later: the service layer. That's where you implement "business" code. And most people want their "business" code to be transactional; if not through database transactions, at least in effect (see below). mixing rest and transactions is a poor design. The same applies to services as well Sure you can avoid transactions in the service layer. And then a service calling multiple (transactional) repository methods would not itself be transactional, requiring additional measures to handle cases like "my second repository call failed and I didn't get to run the third one, but the first one was committed". Compensating transactions in SAGA are an example of such additional measures. Anyway, I won't argue more than this. I'm not an expert in application architecture, and there are too many different views on it for this discussion to matter. There are people advocating for no DB transaction and SAGA patterns everywhere, though I'm not sure there are many who say this is the way to go for every application. Bottom line, from my point of view: you're entitled to your opinions, but at the very least I wouldn't present putting But in this case one needs to override all the write operations (persist, update, delete, in all their flavors) which might be fastidious and repetitive. It sure is. It's also rather pointless, because these operations often involve a single SQL statement anyway, and will automatically open a (short-lived, implicit) transaction on the database side. So you would only be making them transactional to make Quarkus happy, with little to no effect in practice. As explained above, your application wouldn't be transactional unless you take additional measures (SAGA, ...). Anyway, feel free to open an issue if you want @FroMage or another Panache author to add the ability to make all repository operations transactional by default. Bắt đầu với khái niệm cơ bản về Git, tìm hiểu về cách cộng tác trong Git, thực hành sử dụng Git và biết thêm về các lệnh Git, tất cả có trong hướng dẫn về Git dành cho người mới bắt đầu của chúng tôi. Chúng tôi khuyên bạn nên bắt đầu lại từ đầu và thực hiện theo cách của bạn. Nếu bạn đã quen thuộc với các khái niệm của Git và muốn bắt tay vào thực hiện, hãy chuyển đến phần “"HCách sử dụng Git"” và làm theo trên Windows, Mac hoặc Dòng lệnh. Subscribe to our newsletterLearn with Nulab about collaboration, design & UX, project management, software development, and strategy & planning. In MongoDB, an operation on a single document is atomic. Because you can use embedded documents and arrays to capture relationships between data in a single document structure instead of normalizing across multiple documents and collections, this single-document atomicity obviates the need for distributed transactions for many practical use cases. For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports distributed transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards. ➤ Use the Select your language drop-down menu in the upper-right to set the language of the following example. TipSee also:For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports distributed transactions, including transactions on replica sets and sharded clusters. Distributed transactions are atomic:
ImportantIn most cases, a distributed transaction incurs a greater performance cost over single document writes, and the availability of distributed transactions should not be a replacement for effective schema design. For many scenarios, the will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for distributed transactions. For additional transactions usage considerations (such as runtime limit and oplog size limit), see also TipSee also:Distributed transactions can be used across multiple operations, collections, databases, documents, and shards. For transactions:
For a list of operations not supported in transactions, see TipWhen creating or dropping a collection immediately before starting a transaction, if the collection is accessed within the transaction, issue the create or drop operation with write concern to ensure that the transaction can acquire the required locks. TipSee also:You can perform the following operations in a if the transaction is not a cross-shard write transaction:
When creating a collection inside a transaction:
When , the index to create must be on either:
TipSee also:To perform a count operation within a transaction, use the aggregation stage or the (with a expression) aggregation stage. MongoDB drivers provide a collection-level API
3 as a helper method that uses the with a expression to perform a count. The
6 API is deprecated. provides the helper method that uses the with a expression to perform a count. To perform a distinct operation within a transaction:
Informational commands, such as , , (and their helper methods) are allowed in transactions; however, they cannot be the first operation in the transaction. Changed in version 4.4. The following operations are not allowed in transactions:
Tip
Operations in a transaction use the transaction-level Using the drivers, you can set the transaction-level at the transaction start:
that contain read operations must use read preference . All operations in a given transaction must route to the same member. Operations in a transaction use the transaction-level read concern. This means a read concern set at the collection and database level is ignored inside the transaction. You can set the transaction-level read concern at the transaction start.
Transactions support the following read concern levels:
Transactions use the transaction-level write concern to commit the write operations. Write operations inside transactions must be run without an explicit write concern specification and use the default write concern. At commit time, the writes committed using the transaction-level write concern. TipDon't explicitly set the write concern for the individual write operations inside a transaction. Setting write concerns for the individual write operations inside a transaction returns an error. You can set the transaction-level write concern at the transaction start:
TipSee also:Transactions support all write concern values, including:
|