Kshitij Kohli
Kshitij Kohli

Reputation: 4393

How to know underlying database name via Spring Data JPA

I am connecting to multiple databases in my spring-boot app using Spring Data JPA by determining which database to connect at runtime. How can I check for validation on which database am I actually connected to on which my queries are running?

Upvotes: 7

Views: 6609

Answers (3)

Vishesh
Vishesh

Reputation: 333

Referred Answer from:

how to properly specify database schema in spring boot?

Just appending the schema name after actual DB host name. For my code MYSQL DB is configured, and this worked fine for me:

appname.datasource.url = jdbc:mysql://DBhostname.com/schema_test?useSSL=false

the key name can be any custom name that suits your application

Upvotes: 0

Robert Niestroj
Robert Niestroj

Reputation: 16151

Only thing i can think of is some like a (simplified)

@Entity
public class ConfigDB{
 Integer id;

 String tenant; 
}

then have a repository:

public ConfigDBRepository extends CrudRepository{
  public boolean existsByTenant(String tenant);
}

And the have in every DB this tenant in this table setup for validation.

Upvotes: -1

hovanessyan
hovanessyan

Reputation: 31483

Try to get the URL from the Datasource, maybe will do it:

dataSource.getConnection().getMetaData().getURL();

Checkout DatabaseMetaData documentation for complete details.

Upvotes: 5

Related Questions