|
| 1 | +# MySQL Session Service for ADK |
| 2 | + |
| 3 | +This sub-module contains an implementation of a session service for the ADK (Agent Development Kit) that uses MySQL as the backend for storing session data. This allows developers to manage user sessions in a scalable and reliable manner using MySQL's relational database capabilities and JSON column support. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +To integrate this MySQL session service into your ADK project, add the following dependencies to your project's build configuration. |
| 8 | + |
| 9 | +### Maven |
| 10 | + |
| 11 | +```xml |
| 12 | +<dependencies> |
| 13 | + <!-- ADK Core --> |
| 14 | + <dependency> |
| 15 | + <groupId>com.google.adk</groupId> |
| 16 | + <artifactId>google-adk</artifactId> |
| 17 | + <version>0.4.1-SNAPSHOT</version> |
| 18 | + </dependency> |
| 19 | + <!-- MySQL Session Service --> |
| 20 | + <dependency> |
| 21 | + <groupId>com.google.adk.contrib</groupId> |
| 22 | + <artifactId>google-adk-mysql-session-service</artifactId> |
| 23 | + <version>0.4.1-SNAPSHOT</version> |
| 24 | + </dependency> |
| 25 | + <!-- MySQL Connector (or your preferred driver) --> |
| 26 | + <dependency> |
| 27 | + <groupId>com.mysql</groupId> |
| 28 | + <artifactId>mysql-connector-j</artifactId> |
| 29 | + <version>8.3.0</version> |
| 30 | + </dependency> |
| 31 | + <!-- HikariCP (Recommended for connection pooling) --> |
| 32 | + <dependency> |
| 33 | + <groupId>com.zaxxer</groupId> |
| 34 | + <artifactId>HikariCP</artifactId> |
| 35 | + <version>5.1.0</version> |
| 36 | + </dependency> |
| 37 | +</dependencies> |
| 38 | +``` |
| 39 | + |
| 40 | +## Database Schema |
| 41 | + |
| 42 | +You must create the following tables in your MySQL database (version 8.0+ required for JSON support). |
| 43 | + |
| 44 | +```sql |
| 45 | +CREATE TABLE IF NOT EXISTS adk_sessions ( |
| 46 | + session_id VARCHAR(255) PRIMARY KEY, |
| 47 | + app_name VARCHAR(255) NOT NULL, |
| 48 | + user_id VARCHAR(255) NOT NULL, |
| 49 | + state JSON, |
| 50 | + created_at TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3), |
| 51 | + updated_at TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), |
| 52 | + INDEX idx_app_user (app_name, user_id) |
| 53 | +); |
| 54 | + |
| 55 | +CREATE TABLE IF NOT EXISTS adk_events ( |
| 56 | + event_id VARCHAR(255) PRIMARY KEY, |
| 57 | + session_id VARCHAR(255) NOT NULL, |
| 58 | + event_data JSON, |
| 59 | + created_at TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3), |
| 60 | + FOREIGN KEY (session_id) REFERENCES adk_sessions(session_id) ON DELETE CASCADE, |
| 61 | + INDEX idx_session_created (session_id, created_at) |
| 62 | +); |
| 63 | + |
| 64 | +CREATE TABLE IF NOT EXISTS adk_app_state ( |
| 65 | + app_name VARCHAR(255) NOT NULL, |
| 66 | + state_key VARCHAR(255) NOT NULL, |
| 67 | + state_value JSON, |
| 68 | + PRIMARY KEY (app_name, state_key) |
| 69 | +); |
| 70 | + |
| 71 | +CREATE TABLE IF NOT EXISTS adk_user_state ( |
| 72 | + app_name VARCHAR(255) NOT NULL, |
| 73 | + user_id VARCHAR(255) NOT NULL, |
| 74 | + state_key VARCHAR(255) NOT NULL, |
| 75 | + state_value JSON, |
| 76 | + PRIMARY KEY (app_name, user_id, state_key) |
| 77 | +); |
| 78 | +``` |
| 79 | + |
| 80 | +## Usage |
| 81 | + |
| 82 | +You can configure the `MySqlSessionService` by passing a `DataSource`. We recommend using HikariCP for production connection pooling. |
| 83 | + |
| 84 | +```java |
| 85 | +import com.google.adk.runner.Runner; |
| 86 | +import com.google.adk.sessions.MySqlSessionService; |
| 87 | +import com.zaxxer.hikari.HikariConfig; |
| 88 | +import com.zaxxer.hikari.HikariDataSource; |
| 89 | +import javax.sql.DataSource; |
| 90 | + |
| 91 | +public class MyApp { |
| 92 | + public static void main(String[] args) { |
| 93 | + // 1. Configure Data Source |
| 94 | + HikariConfig config = new HikariConfig(); |
| 95 | + config.setJdbcUrl("jdbc:mysql://localhost:3306/my_db"); |
| 96 | + config.setUsername("user"); |
| 97 | + config.setPassword("password"); |
| 98 | + DataSource dataSource = new HikariDataSource(config); |
| 99 | + |
| 100 | + // 2. Create the Session Service |
| 101 | + MySqlSessionService sessionService = new MySqlSessionService(dataSource); |
| 102 | + |
| 103 | + // 3. Pass it to the Runner |
| 104 | + Runner runner = new Runner( |
| 105 | + new MyAgent(), |
| 106 | + "MyAppName", |
| 107 | + new InMemoryArtifactService(), // or GcsArtifactService |
| 108 | + sessionService, // <--- Your MySQL Service |
| 109 | + new InMemoryMemoryService() // or FirestoreMemoryService |
| 110 | + ); |
| 111 | + |
| 112 | + runner.runLive(); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Testing |
| 118 | + |
| 119 | +This module includes both unit tests and integration tests. |
| 120 | + |
| 121 | +* **Unit Tests:** Fast, in-memory tests that mock the database connection. These verify the service logic without requiring a running database. |
| 122 | + ```bash |
| 123 | + mvn test |
| 124 | + ``` |
| 125 | + |
| 126 | +* **Integration Tests:** Comprehensive tests that run against a real MySQL instance using [Testcontainers](https://testcontainers.com/). These require a Docker environment (e.g., Docker Desktop, or a CI runner with Docker support). |
| 127 | + ```bash |
| 128 | + mvn verify |
| 129 | + ``` |
| 130 | + |
0 commit comments