-
Hi, could someone give an example how to use the wrapper with HikariCP + the IAM authentication plugin enabled? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @felixdo, you can enable plugins by specifying the Here is a simple example: public static void main(String[] args) throws SQLException {
try (HikariDataSource ds = new HikariDataSource()) {
// Configure the connection pool:
ds.setUsername(IAM_DATABASE_USER);
// Specify the underlying datasource for HikariCP:
ds.setDataSourceClassName(AwsWrapperDataSource.class.getName());
// Configure AwsWrapperDataSource:
ds.addDataSourceProperty("jdbcProtocol", "jdbc:postgresql:");
ds.addDataSourceProperty("serverName", "db-identifier.cluster-XYZ.us-east-2.rds.amazonaws.com");
ds.addDataSourceProperty("serverPort", "5432");
ds.addDataSourceProperty("database", "postgres");
// The failover plugin throws failover-related exceptions that need to be handled explicitly by HikariCP,
// otherwise connections will be closed immediately after failover. Set `ExceptionOverrideClassName` to provide
// a custom exception class.
ds.setExceptionOverrideClassName("software.amazon.jdbc.util.HikariCPSQLException");
// Specify the driver-specific data source for AwsWrapperDataSource:
ds.addDataSourceProperty("targetDataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");
Properties targetDataSourceProps = new Properties();
// Enable the IAM authentication plugin along with failover and host monitoring plugins.
targetDataSourceProps.setProperty("wrapperPlugins", "iam,failover,efm");
ds.addDataSourceProperty("targetDataSourceProperties", targetDataSourceProps);
// Attempt a connection:
try (final Connection conn = ds.getConnection();
final Statement statement = conn.createStatement();
final ResultSet rs = statement.executeQuery("SELECT * from aurora_db_instance_identifier()")) {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
} This example derives from HikariFailoverExample.java. |
Beta Was this translation helpful? Give feedback.
-
I tried this suggestion and am getting a PAM authentication error. I am using AWS Aurora Postgres database. I tried as @felixdo has it, and I also tried assuming a role first. Both had the same PAM authentication error.
I am able to connect if I assume a role and generate my own token and not use the AwsWrapperDataSource, though. |
Beta Was this translation helpful? Give feedback.
Hi @felixdo, you can enable plugins by specifying the
wrapperPlugins
parameter.Here is a simple example: