# 多数据源

提供动态数据源支持功能,支持注解方式,编程方式动态切换数据源,支持事务中切换数据源,支持跨数据库事务。

## 声明式数据源切换

{% code title="pom.xml" %}

```yaml
hsweb:
    datasource:
        switcher:
           test: # 只是一个标识
              # 拦截类和方法的表达式
              expression: org.hswebframework.**.*Service.find*
              # 使用数据源
              data-source-id: read_db
```

{% endcode %}

## 注解方式数据源切换

```java
@UseDataSource("mysql_write_01")
String insert(MyEntity);

@UseDataSource("mysql_read_01")
MyEntity selectByPk(String id);

@UseDefaultDataSource()
MyEntity selectByPk(String id);
```

## 编程式数据源切换

```java
  //切换到 id为mysql_read_01的数据源
  DataSourceHolder.switcher().use("mysql_read_01");
  // ....
  //切换到 id为mysql_write_01的数据源
  DataSourceHolder.switcher().use("mysql_write_01");
  // ....
  // 切换到上一次使用的数据源 (mysql_read_01)
   DataSourceHolder.switcher().useLast();
  // ...
  // 切换到默认的数据源
  DataSourceHolder.switcher().useDefault();
```

{% hint style="danger" %}
如果没有使用[Jta多数据源,](/framework/duo-shu-ju-yuan/jta-duo-shu-ju-yuan-shi-wu.md)则无法在事务中切换数据源, 你可能需要先取消掉对应方法上的事务:如在方法上注解`@Transactional(propagation = Propagation.NOT_SUPPORTED)`
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hsweb.io/framework/duo-shu-ju-yuan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
