adk-go icon indicating copy to clipboard operation
adk-go copied to clipboard

Database connections across services

Open verdverm opened this issue 1 month ago • 3 comments

Was looking at the SessionService database implementation and noticed the database connection is opened by the service. It looks like this prevents me from opening a single database client and passing it to all the services (others still lack implementations)

func NewSessionService(dialector gorm.Dialector, opts ...gorm.Option) (session.Service, error) {
	db, err := gorm.Open(dialector, opts...)
	if err != nil {
		return nil, fmt.Errorf("error creating database session service: %w", err)
	}
	return &databaseService{db: db}, nil
}

so I changed it to look like this

func NewSessionService(db *gorm.DB) (session.Service, error) {
	return &databaseService{db: db}, nil
}

Another benefit to this is that my application can also use the same database connection for any extra tables it needs.

Thoughts on the change and the issue more generally?

verdverm avatar Nov 22 '25 20:11 verdverm

+1 that we should take an existing connection instead of opening a connection in NewSessionService.

rakyll avatar Nov 24 '25 00:11 rakyll

SGTM, I'd only introduce a new method for backward compatibility. Are you open for contribution?

mazas-google avatar Nov 24 '25 15:11 mazas-google

@mazas-google both sound good, I'll make sure my CLA is up to date with Google and ship a small PR

any suggestions for the new name?

verdverm avatar Nov 25 '25 06:11 verdverm