Skip to content

Commit 89c8fe5

Browse files
feat: Add DatabaseSessionService with Hibernate and Flyway support
Implements a production-ready database-backed session service that provides persistent storage for ADK sessions, events, and state using JPA/Hibernate. Key features: - Hibernate 6.6 + HikariCP connection pooling for optimal performance - Flyway migrations for schema versioning and zero-downtime deployments - Support for PostgreSQL, MySQL, H2, and other RDBMS - Thread-safe operations with pessimistic locking for concurrent updates - Comprehensive test coverage with H2 in-memory database - Dialect-aware JSON storage (JSONB for PostgreSQL, CLOB for others) - Configurable event filtering and pagination Database dependencies are non-optional in core module for ease of use, consistent with Python ADK's approach where DatabaseSessionService is in the main package.
1 parent 2906eb5 commit 89c8fe5

30 files changed

+5683
-1
lines changed

core/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@
104104
<groupId>com.fasterxml.jackson.dataformat</groupId>
105105
<artifactId>jackson-dataformat-yaml</artifactId>
106106
</dependency>
107+
<!-- Jackson JAXB annotations module for ObjectMapper.findAndRegisterModules() -->
108+
<dependency>
109+
<groupId>com.fasterxml.jackson.module</groupId>
110+
<artifactId>jackson-module-jaxb-annotations</artifactId>
111+
</dependency>
112+
<!-- JAXB API needed by jackson-module-jaxb-annotations on modern JDKs -->
113+
<dependency>
114+
<groupId>jakarta.xml.bind</groupId>
115+
<artifactId>jakarta.xml.bind-api</artifactId>
116+
<version>4.0.0</version>
117+
</dependency>
107118
<dependency>
108119
<groupId>com.google.protobuf</groupId>
109120
<artifactId>protobuf-java</artifactId>
@@ -189,6 +200,70 @@
189200
<artifactId>opentelemetry-sdk-testing</artifactId>
190201
<scope>test</scope>
191202
</dependency>
203+
204+
<!-- Database Session Service Dependencies -->
205+
206+
<!-- Hibernate ORM - JPA implementation for object-relational mapping -->
207+
<dependency>
208+
<groupId>org.hibernate.orm</groupId>
209+
<artifactId>hibernate-core</artifactId>
210+
</dependency>
211+
212+
<!-- Jakarta Persistence API - Standard JPA annotations and interfaces -->
213+
<dependency>
214+
<groupId>jakarta.persistence</groupId>
215+
<artifactId>jakarta.persistence-api</artifactId>
216+
</dependency>
217+
218+
<!-- HikariCP - High-performance JDBC connection pool -->
219+
<dependency>
220+
<groupId>com.zaxxer</groupId>
221+
<artifactId>HikariCP</artifactId>
222+
</dependency>
223+
224+
<!-- Hibernate HikariCP Integration - Enables HikariCP as Hibernate's connection provider -->
225+
<dependency>
226+
<groupId>org.hibernate.orm</groupId>
227+
<artifactId>hibernate-hikaricp</artifactId>
228+
</dependency>
229+
230+
<!-- Database Drivers -->
231+
232+
<!-- H2 Database - In-memory database for testing -->
233+
<dependency>
234+
<groupId>com.h2database</groupId>
235+
<artifactId>h2</artifactId>
236+
<scope>test</scope>
237+
</dependency>
238+
239+
<!-- PostgreSQL Driver - JDBC driver for PostgreSQL databases (optional for production use) -->
240+
<dependency>
241+
<groupId>org.postgresql</groupId>
242+
<artifactId>postgresql</artifactId>
243+
<optional>true</optional>
244+
</dependency>
245+
246+
<!-- MySQL Driver - JDBC driver for MySQL databases (optional for production use) -->
247+
<dependency>
248+
<groupId>com.mysql</groupId>
249+
<artifactId>mysql-connector-j</artifactId>
250+
<optional>true</optional>
251+
</dependency>
252+
253+
<!-- Database Migration -->
254+
255+
<!-- Flyway Core - Database schema migration and versioning tool -->
256+
<dependency>
257+
<groupId>org.flywaydb</groupId>
258+
<artifactId>flyway-core</artifactId>
259+
</dependency>
260+
261+
<!-- Flyway PostgreSQL Support - PostgreSQL-specific migration support -->
262+
<dependency>
263+
<groupId>org.flywaydb</groupId>
264+
<artifactId>flyway-database-postgresql</artifactId>
265+
<scope>runtime</scope>
266+
</dependency>
192267
</dependencies>
193268
<build>
194269
<resources>

core/src/main/java/com/google/adk/runner/Runner.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@
5656
import java.util.Optional;
5757
import java.util.concurrent.ConcurrentHashMap;
5858
import javax.annotation.Nullable;
59+
import org.slf4j.Logger;
60+
import org.slf4j.LoggerFactory;
5961

6062
/** The main class for the GenAI Agents runner. */
6163
public class Runner {
64+
private static final Logger logger = LoggerFactory.getLogger(Runner.class);
6265
private final BaseAgent agent;
6366
private final String appName;
6467
private final BaseArtifactService artifactService;
@@ -542,7 +545,16 @@ public Flowable<Event> runLive(
542545
invocationContext
543546
.agent()
544547
.runLive(invocationContext)
545-
.doOnNext(event -> this.sessionService.appendEvent(session, event))
548+
.flatMap(
549+
event ->
550+
this.sessionService
551+
.appendEvent(session, event)
552+
.toFlowable()
553+
.onErrorResumeNext(
554+
error -> {
555+
logger.warn("Failed to append event to session", error);
556+
return Flowable.just(event);
557+
}))
546558
.onErrorResumeNext(
547559
throwable -> {
548560
span.setStatus(StatusCode.ERROR, "Error in runLive Flowable execution");

0 commit comments

Comments
 (0)