Skip to content

Commit

Permalink
feat: fix Casbin Starter Can't Use Dynamic Data Source (#80)
Browse files Browse the repository at this point in the history
* fix: fix Can't Use Dynamic Data Source

* fix: fix Can't Use Dynamic Data Source
  • Loading branch information
D1zzzY1 authored Jul 20, 2023
1 parent 014277c commit de1fdaa
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,24 @@ public Adapter autoConfigJdbcAdapter(
DataSourceProperties dataSourceProperties
) throws Exception {
JdbcTemplate jdbcTemplateToUse = getJdbcTemplate(jdbcTemplate, casbinDataSource);
if (jdbcTemplateToUse == null || jdbcTemplateToUse.getDataSource() == null) {
throw new CasbinAdapterException("Cannot create jdbc adapter, because jdbc template is not set");
}
String databaseName = getDatabaseName(jdbcTemplateToUse.getDataSource());
CasbinDataSourceInitializationMode initializeSchema = properties.getInitializeSchema();
boolean autoCreateTable = initializeSchema == CasbinDataSourceInitializationMode.CREATE;
String tableName = properties.getTableName();
logger.info("Casbin current use database product: {}", databaseName);
return new JDBCAdapter(dataSourceProperties.determineDriverClassName(), dataSourceProperties.getUrl(),
dataSourceProperties.getUsername(), dataSourceProperties.getPassword(),
exceptionProperties.isRemovePolicyFailed(), tableName, autoCreateTable);

// datasource properties are configed
if (dataSourceProperties.getUrl() != null) {
return new JDBCAdapter(dataSourceProperties.determineDriverClassName(), dataSourceProperties.getUrl(),
dataSourceProperties.getUsername(), dataSourceProperties.getPassword(),
exceptionProperties.isRemovePolicyFailed(), tableName, autoCreateTable);
}
// when datasource properties are not configed, use the default datasource in jdbcTemplate
else {
return new JDBCAdapter(jdbcTemplateToUse.getDataSource(), exceptionProperties.isRemovePolicyFailed(), tableName, autoCreateTable);
}
}

/**
Expand Down

0 comments on commit de1fdaa

Please sign in to comment.