W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
SpannerOperations
提供了在單個(gè)事務(wù)中運(yùn)行java.util.Function
對(duì)象的方法,同時(shí)使來(lái)自SpannerOperations
的讀取和寫入方法可用。
SpannerOperations
通過(guò)performReadWriteTransaction
方法提供讀寫事務(wù):
@Autowired SpannerOperations mySpannerOperations; public String doWorkInsideTransaction() { return mySpannerOperations.performReadWriteTransaction( transActionSpannerOperations -> { // Work with transActionSpannerOperations here. // It is also a SpannerOperations object. return "transaction completed"; } ); }
performReadWriteTransaction
方法接受Function
對(duì)象,該對(duì)象提供了SpannerOperations
對(duì)象的實(shí)例。函數(shù)的最終返回值和類型由用戶確定。
您可以像常規(guī)的SpannerOperations
一樣使用此對(duì)象,但有一些例外:
performReadWriteTransaction
或performReadOnlyTransaction
執(zhí)行子交易。
由于這些讀寫事務(wù)正在鎖定,因此如果函數(shù)不執(zhí)行任何寫操作,則建議您使用performReadOnlyTransaction
。
performReadOnlyTransaction
方法用于使用SpannerOperations
執(zhí)行只讀事務(wù):
@Autowired SpannerOperations mySpannerOperations; public String doWorkInsideTransaction() { return mySpannerOperations.performReadOnlyTransaction( transActionSpannerOperations -> { // Work with transActionSpannerOperations here. // It is also a SpannerOperations object. return "transaction completed"; } ); }
performReadOnlyTransaction
方法接受提供SpannerOperations
對(duì)象實(shí)例的Function
。此方法還接受ReadOptions
對(duì)象,但是唯一使用的屬性是用于及時(shí)確定快照以在事務(wù)中執(zhí)行讀取的時(shí)間戳記。
如果未在讀取選項(xiàng)中設(shè)置時(shí)間戳,則將針對(duì)數(shù)據(jù)庫(kù)的當(dāng)前狀態(tài)運(yùn)行事務(wù)。函數(shù)的最終返回值和類型由用戶確定。您可以像使用普通SpannerOperations
一樣使用此對(duì)象,但有一些例外:
performReadWriteTransaction
或performReadOnlyTransaction
執(zhí)行子交易
由于只讀事務(wù)是非鎖定的,并且可以在過(guò)去的某個(gè)時(shí)間點(diǎn)執(zhí)行,因此建議將這些事務(wù)用于不執(zhí)行寫操作的功能。
此功能需要使用spring-cloud-gcp-starter-data-spanner
時(shí)提供的SpannerTransactionManager
中的bean。
SpannerTemplate
和SpannerRepository
通過(guò)@Transactional
[注釋](https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#交易聲明式)作為交易。如果用@Transactional
注釋的方法調(diào)用了也注釋的另一個(gè)方法,則這兩種方法將在同一事務(wù)中工作。
performReadOnlyTransaction
和performReadWriteTransaction
無(wú)法在帶注釋的@Transactional
方法中使用,因?yàn)镃loud Spanner不支持事務(wù)內(nèi)的事務(wù)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: