

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用適用於 Java 的 X-Ray 開發套件追蹤 SQL 查詢
<a name="xray-sdk-java-sqlclients"></a>

**注意**  
X-Ray 開發套件/協助程式維護通知 – 在 2026 年 2 月 25 日， AWS X-Ray SDKs/協助程式將進入維護模式，其中 AWS 將限制 X-Ray 開發套件和協助程式版本，以僅解決安全問題。如需支援時間軸的詳細資訊，請參閱 [X-Ray SDK 和協助程式支援時間表](xray-sdk-daemon-timeline.md)。建議您遷移至 OpenTelemetry。如需遷移至 OpenTelemetry 的詳細資訊，請參閱[從 X-Ray 檢測遷移至 OpenTelemetry 檢測](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-migration.html)。

## SQL 攔截器
<a name="xray-sdk-java-sqlclients-interceptors"></a>

將適用於 Java JDBC 的 X-Ray 開發套件攔截器新增至資料來源組態，以檢測 SQL 資料庫查詢。
+  **PostgreSQL** – `com.amazonaws.xray.sql.postgres.TracingInterceptor` 
+  **MySQL** – `com.amazonaws.xray.sql.mysql.TracingInterceptor` 

這些攔截器分別位於 [`aws-xray-recorder-sql-postgres` 和 `aws-xray-recorder-sql-mysql` 子模組](xray-sdk-java.md)中。他們會實作 `org.apache.tomcat.jdbc.pool.JdbcInterceptor`，並且與 Tomcat 連線集區相容。

**注意**  
基於安全性目的，SQL 攔截器不會在子區段內記錄 SQL 查詢本身。

針對 Spring，請在屬性檔案中新增攔截器，然後使用 Spring Boot 的 `DataSourceBuilder` 建置資料來源。

**Example `src/main/java/resources/application.properties` - PostgreSQL JDBC 攔截器**  

```
spring.datasource.continue-on-error=true
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.jdbc-interceptors=com.amazonaws.xray.sql.postgres.TracingInterceptor
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL94Dialect
```

**Example `src/main/java/myapp/WebConfig.java` - 資料來源**  

```
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import javax.servlet.Filter;
import javax.sql.DataSource;
import java.net.URL;

@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories("myapp")
public class RdsWebConfig {

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource")
  public DataSource dataSource() {
      logger.info("Initializing PostgreSQL datasource");
      return DataSourceBuilder.create()
              .driverClassName("org.postgresql.Driver")
              .url("jdbc:postgresql://" + System.getenv("RDS_HOSTNAME") + ":" + System.getenv("RDS_PORT") + "/ebdb")
              .username(System.getenv("RDS_USERNAME"))
              .password(System.getenv("RDS_PASSWORD"))
              .build();
  }
...
}
```

對於 Tomcat，請在 JDBC 資料來源`setJdbcInterceptors`上呼叫 ，並參考適用於 Java 的 X-Ray 開發套件類別。

**Example `src/main/myapp/model.java` - 資料來源**  

```
import org.apache.tomcat.jdbc.pool.DataSource;
...
DataSource source = new DataSource();
source.setUrl(url);
source.setUsername(user);
source.setPassword(password);
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setJdbcInterceptors("com.amazonaws.xray.sql.mysql.TracingInterceptor;");
```

Tomcat JDBC 資料來源程式庫包含在適用於 Java 的 X-Ray 開發套件中，但您可以將其宣告為提供相依性，以記錄您使用它。

**Example `pom.xml` - JDBC 資料來源**  

```
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jdbc</artifactId>
  <version>8.0.36</version>
  <scope>provided</scope>
</dependency>
```

## 原生 SQL 追蹤裝飾項目
<a name="xray-sdk-java-sqlclients-nativeSQL"></a>
+ 將 [https://github.com/aws/aws-xray-sdk-java/tree/master/aws-xray-recorder-sdk-sql](https://github.com/aws/aws-xray-sdk-java/tree/master/aws-xray-recorder-sdk-sql)新增至您的相依性。
+ 裝飾資料庫資料來源、連線或陳述式。

  ```
  dataSource = TracingDataSource.decorate(dataSource)
  connection = TracingConnection.decorate(connection)
  statement = TracingStatement.decorateStatement(statement)
  preparedStatement = TracingStatement.decoratePreparedStatement(preparedStatement, sql)
  callableStatement = TracingStatement.decorateCallableStatement(callableStatement, sql)
  ```