Skip to content

Commit 71e9068

Browse files
author
lucas
committed
feat: Add MySQL Session Service implementation
1 parent 5d8f85b commit 71e9068

File tree

8 files changed

+1520
-0
lines changed

8 files changed

+1520
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2025 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<parent>
23+
<groupId>com.google.adk</groupId>
24+
<artifactId>google-adk-parent</artifactId>
25+
<version>0.4.1-SNAPSHOT</version>
26+
<relativePath>../../pom.xml</relativePath>
27+
</parent>
28+
29+
<artifactId>google-adk-mysql-session-service</artifactId>
30+
<name>Agent Development Kit - MySQL Session Management</name>
31+
<description>MySQL integration with Agent Development Kit for User Session Management</description>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>com.google.adk</groupId>
36+
<artifactId>google-adk</artifactId>
37+
<version>${project.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.google.genai</groupId>
41+
<artifactId>google-genai</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>com.mysql</groupId>
46+
<artifactId>mysql-connector-j</artifactId>
47+
<version>8.3.0</version>
48+
</dependency>
49+
50+
<!-- Testing -->
51+
<dependency>
52+
<groupId>com.google.truth</groupId>
53+
<artifactId>truth</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.mockito</groupId>
58+
<artifactId>mockito-core</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.junit.jupiter</groupId>
63+
<artifactId>junit-jupiter-api</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.junit.jupiter</groupId>
68+
<artifactId>junit-jupiter-engine</artifactId>
69+
<scope>test</scope>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>org.testcontainers</groupId>
74+
<artifactId>mysql</artifactId>
75+
<version>1.19.7</version>
76+
<scope>test</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.testcontainers</groupId>
80+
<artifactId>junit-jupiter</artifactId>
81+
<version>1.19.7</version>
82+
<scope>test</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.zaxxer</groupId>
86+
<artifactId>HikariCP</artifactId>
87+
<version>5.1.0</version>
88+
<scope>test</scope>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.slf4j</groupId>
92+
<artifactId>slf4j-simple</artifactId>
93+
<version>2.0.17</version>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>com.h2database</groupId>
98+
<artifactId>h2</artifactId>
99+
<version>2.2.224</version>
100+
<scope>test</scope>
101+
</dependency>
102+
</dependencies>
103+
<build>
104+
<plugins>
105+
<plugin>
106+
<groupId>org.apache.maven.plugins</groupId>
107+
<artifactId>maven-failsafe-plugin</artifactId>
108+
<version>3.2.5</version>
109+
<executions>
110+
<execution>
111+
<goals>
112+
<goal>integration-test</goal>
113+
<goal>verify</goal>
114+
</goals>
115+
</execution>
116+
</executions>
117+
</plugin>
118+
</plugins>
119+
</build>
120+
</project>

0 commit comments

Comments
 (0)