Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to set up a lambda to initialize new connections #705

Merged
merged 9 commits into from
Oct 27, 2023

Conversation

sergiyvamz
Copy link
Contributor

Summary

Allow user to set up a lambda to initialize new connections.

Description

User needs to set an connection initialization function with ConnectionProviderManager.setConnectionInitFunc()
Related to #679

Additional Reviewers

@karenc-bq
@crystall-bitquill
@brunos-bq
@aaronchung-bitquill

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@davecramer
Copy link
Contributor

docs ?

@sergiyvamz sergiyvamz changed the title [WIP] allow user to set up a lambda to initialize new connections Allow user to set up a lambda to initialize new connections Oct 26, 2023

ConnectionProviderManager.setConnectionInitFunc((connection, protocol, hostSpec, props) -> {
if ("test-database".equals(props.getProperty("database"))) {
connection.setSchema("test-database-schema");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: assert schema is set for the connection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops. That's not supposed to be here.

docs/using-the-jdbc-driver/UsingTheJdbcDriver.md Outdated Show resolved Hide resolved
### Connection Initialization
In some cases it's necessary to configure a connection before a user application can use it. Some target drivers provides such functionality and allow to specify a configuration parameter with SQL statements that are executed when connection is established. However, not all drivers supports such functionality. Also, in some cases, additional conditions should be checked in order to identify what initialization is required for a particular connection.

AWS JDBC Driver allows to specify a special function that can initialize a connection. It can be done with `ConnectionProviderManager.setConnectionInitFunc` method. `resetConnectionInitFunc` method is also available.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AWS JDBC Driver allows users to specify a specific function that will be executed every time a connection is initialized. This function needs to be wrapped in a ConnectionInitFunc object, that implements the initConnection() method. Once that object is defined, it is passed to the driver using the ConnectionProviderManager.setConnectionInitFunc method.

It is also possible to stop/reset executing this function by using the resetConnectionInitFunc method.

docs/using-the-jdbc-driver/UsingTheJdbcDriver.md Outdated Show resolved Hide resolved
> :warning: Executing CPU and network intensive code in the initialization function may cause significant performance degradation.

```java
ConnectionProviderManager.setConnectionInitFunc((connection, protocol, hostSpec, props) -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: let's also add a case without a lambda

docs/using-the-jdbc-driver/UsingTheJdbcDriver.md Outdated Show resolved Hide resolved
@@ -100,6 +100,25 @@ DriverConfigurationProfiles.addOrReplaceProfile(
CustomConnectionPluginFactory.class));
```

### Connection Initialization
In some cases it's necessary to configure a connection before a user application can use it. Some target drivers provides such functionality and allow to specify a configuration parameter with SQL statements that are executed when connection is established. However, not all drivers supports such functionality. Also, in some cases, additional conditions should be checked in order to identify what initialization is required for a particular connection.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some particular cases, users may need to define a specific configuration for a connection that has just been opened by the driver before an application can use it.

Some...

@sergiyvamz sergiyvamz merged commit bf9b25c into main Oct 27, 2023
5 checks passed
@sergiyvamz sergiyvamz deleted the init-connection branch October 27, 2023 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants