Reputation: 4393
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
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
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
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