當前位置:首頁 » 操作系統 » 跨資料庫的事務

跨資料庫的事務

發布時間: 2024-06-26 07:16:58

① mssql如何將一個資料庫中的表同步到另一個資料庫中 做成事務

1 在另一個資料庫中建立同樣結構的副表,導入相同的數據
2 在本地資料庫建立另一個資料庫的相應鏈接
3 在本地資料庫主表建立插入修改刪除觸發器,主表有什麼變化直接寫入到副表中
4當然這種方法也不是特別好,會影響本地資料庫主表的性能。

② 如何在spring中配置跨資料庫的事務

1.使用註解的方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 使用annotation定義資料庫事務,這樣可以在類或方法中直接使用@Transactional註解來聲明事務 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

2.使用事務管理器來管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<aop:config>
<!-- 切入點指明了在執行com.zxt.service包中的所有方法時產生事務攔截操作 -->
<aop:pointcut id="Methods"
expression="execution(* com.zxt.service.*.*(..))" />
<!-- 定義了將採用何種攔截操作,這里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="Methods" />
</aop:config>

<!-- 事務通知操作,使用的事務管理器引用自transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事務 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<!-- read-only="true":其餘方法只讀格式,加強其安全性 -->
<tx:method name="*" read-only="true" propagation="NOT_SUPPORTED" />
</tx:attributes>
</tx:advice>

</beans>

③ 什麼是CAP原理

分布式領域CAP理論,
Consistency(一致性), 數據一致更新,所有數據變動都是同步的
Availability(可用性), 好的響應性能
Partition tolerance(分區容錯性) 可靠性

定理:任何分布式系統只可同時滿足二點,沒法三者兼顧。
忠告:架構師不要將精力浪費在如何設計能滿足三者的完美分布式系統,而是應該進行取捨。

關系資料庫的ACID模型擁有 高一致性 + 可用性 很難進行分區:
Atomicity原子性:一個事務中所有操作都必須全部完成,要麼全部不完成。
Consistency一致性. 在事務開始或結束時,資料庫應該在一致狀態。
Isolation隔離層. 事務將假定只有它自己在操作資料庫,彼此不知曉。
Durability. 一旦事務完成,就不能返回。
跨資料庫事務:2PC (two-phase commit), 2PC is the anti-scalability pattern (Pat Helland) 是反可伸縮模式的,JavaEE中的JTA事務可以支持2PC。因為2PC是反模式,盡量不要使用2PC,使用BASE來迴避。

BASE模型反ACID模型,完全不同ACID模型,犧牲高一致性,獲得可用性或可靠性:
Basically Available基本可用。支持分區失敗(e.g. sharding碎片劃分資料庫)
Soft state軟狀態 狀態可以有一段時間不同步,非同步。
Eventually consistent最終一致,最終數據是一致的就可以了,而不是時時高一致。

BASE思想的主要實現有
1.按功能劃分資料庫
2.sharding碎片

BASE思想主要強調基本的可用性,如果你需要High 可用性,也就是純粹的高性能,那麼就要以一致性或容錯性為犧牲,BASE思想的方案在性能上還是有潛力可挖的。

現在NoSQL運動豐富了拓展了BASE思想,可按照具體情況定製特別方案,比如忽視一致性,獲得高可用性等等,NOSQL應該有下面兩個流派:
1. Key-Value存儲,如Amaze Dynamo等,可根據CAP三原則靈活選擇不同傾向的資料庫產品。
2. 領域模型 + 分布式緩存 + 存儲 (Qi4j和NoSQL運動),可根據CAP三原則結合自己項目定製靈活的分布式方案,難度高。

這兩者共同點:都是關系資料庫SQL以外的可選方案,邏輯隨著數據分布,任何模型都可以自己持久化,將數據處理和數據存儲分離,將讀和寫分離,存儲可以是非同步或同步,取決於對一致性的要求程度。

不同點:NOSQL之類的Key-Value存儲產品是和關系資料庫頭碰頭的產品BOX,可以適合非Java如PHP RUBY等領域,是一種可以拿來就用的產品,而領域模型 + 分布式緩存 + 存儲是一種復雜的架構解決方案,不是產品,但這種方式更靈活,更應該是架構師必須掌握的。

熱點內容
mysql創建表的sql語句 發布:2024-09-29 04:24:46 瀏覽:328
protues用什麼編譯器 發布:2024-09-29 04:04:12 瀏覽:421
bab編程 發布:2024-09-29 03:48:58 瀏覽:933
魔獸世界伺服器新是什麼意思 發布:2024-09-29 03:43:48 瀏覽:390
吉利博越自動擋哪個配置最好 發布:2024-09-29 03:43:26 瀏覽:761
伺服器出現故障碼怎麼解決 發布:2024-09-29 03:40:50 瀏覽:182
公費訪問學者 發布:2024-09-29 03:33:12 瀏覽:309
雲主機源碼 發布:2024-09-29 03:18:28 瀏覽:663
cspython 發布:2024-09-29 02:58:07 瀏覽:738
下載加密日記軟體 發布:2024-09-29 02:58:07 瀏覽:800