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

[Feature][Jdbc] Supporting more ways to configure connection parameters. #5388

Merged
merged 31 commits into from
Oct 18, 2023

Conversation

Carl-Zhou-CN
Copy link
Member

Mainly to deal with databases like Oracle that cannot concatenate parameters in the URL.

Purpose of this pull request

Check list

driver = "com.mysql.cj.jdbc.Driver"
connection_check_timeout_sec = 100
user = "root"
password = "Abc!@#135_seatunnel"

query = "select * from source;"
properties {
useSSL=false
rewriteBatchedStatements=true
Copy link
Contributor

Choose a reason for hiding this comment

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

rewriteBatchedStatements=true Can you add this configuration to option by default? If the user does not set it manually, we can also enable this optimization in the demo

Copy link
Member Author

Choose a reason for hiding this comment

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

I have already added it. Please take a look and let me know if it meets the requirements.

Copy link
Member

Choose a reason for hiding this comment

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

This is I want too. Set by default rather than teach user to configure rewriteBatchedStatements is more reasonable.

@EricJoy2048
Copy link
Member

Please resolve conflicts.

Copy link
Member

@EricJoy2048 EricJoy2048 left a comment

Choose a reason for hiding this comment

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

LGTM, Please resolve the conflicts.

@EricJoy2048
Copy link
Member

LGTM, Please resolve the conflicts.

I am sorry, document need be update.

@Carl-Zhou-CN
Copy link
Member Author

LGTM, Please resolve the conflicts.

I am sorry, document need be update.

@EricJoy2048 done

@EricJoy2048 EricJoy2048 added this to the 2.3.4 milestone Sep 15, 2023
@zhilinli123
Copy link
Contributor

zhilinli123 commented Sep 18, 2023

Can you provide screenshots for this? Make sure he's valid
@Carl-Zhou-CN
CC @hailin0 @ic4y

Copy link
Member

@EricJoy2048 EricJoy2048 left a comment

Choose a reason for hiding this comment

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

LGTM, A little problem.

@@ -72,6 +72,7 @@ semantics (using XA transaction guarantee).
| max_commit_attempts | Int | No | 3 | The number of retries for transaction commit failures |
| transaction_timeout_sec | Int | No | -1 | The timeout after the transaction is opened, the default is -1 (never timeout). Note that setting the timeout may affect<br/>exactly-once semantics |
| auto_commit | Boolean | No | true | Automatic transaction commit is enabled by default |
| properties | | No | - | Additional connection configuration parameters |
Copy link
Member

Choose a reason for hiding this comment

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

The type of properties is map ?

@@ -48,6 +48,7 @@ support `Xa transactions`. You can set `is_exactly_once=true` to enable it.
| transaction_timeout_sec | Int | No | -1 |
| auto_commit | Boolean | No | true |
| field_ide | String | No | - |
| properties | | No | - |
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

@Carl-Zhou-CN
Copy link
Member Author

Can you provide screenshots for this? Make sure he's valid @Carl-Zhou-CN CC @hailin0 @ic4y

image
image

EricJoy2048
EricJoy2048 previously approved these changes Sep 18, 2023
Copy link
Member

@EricJoy2048 EricJoy2048 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@Hisoka-X Hisoka-X left a comment

Choose a reason for hiding this comment

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

Over all looking great. But could you add some UT to cover some case?

  1. User configure parameter in both url and properties with different key.
  2. User configure parameter in both url and properties with same key different value (also should metion it in the doc which value will be choose, I think the level of url should be higher).
  3. Only properties or only in url.
    With these case, you should use UT to check the parameter result is you want.

@Carl-Zhou-CN
Copy link
Member Author

Over all looking great. But could you add some UT to cover some case?

  1. User configure parameter in both url and properties with different key.
  2. User configure parameter in both url and properties with same key different value (also should metion it in the doc which value will be choose, I think the level of url should be higher).
  3. Only properties or only in url.
    With these case, you should use UT to check the parameter result is you want.

done

@@ -133,6 +142,7 @@ public static final class Builder {
private String xaDataSourceClassName;
private int maxCommitAttempts = JdbcOptions.MAX_COMMIT_ATTEMPTS.defaultValue();
private int transactionTimeoutSec = JdbcOptions.TRANSACTION_TIMEOUT_SEC.defaultValue();
private Map<String, String> info;
Copy link
Member

Choose a reason for hiding this comment

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

Why not call it properties?

Suggested change
private Map<String, String> info;
private Map<String, String> properties;


void defaultSinkParametersTest() throws IOException, SQLException, ClassNotFoundException {
// case1 url not contains parameters and properties not contains parameters
JdbcSink jdbcSink1 = new JdbcSink();
Copy link
Member

Choose a reason for hiding this comment

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

Please use JdbcSinkFactory to create JdbcSink. We will remove prepare method in the future.

Assertions.assertEquals(connectionProperties1.get("rewriteBatchedStatements"), "true");

// case2 url contains parameters and properties not contains parameters
JdbcSink jdbcSink2 = new JdbcSink();
Copy link
Member

Choose a reason for hiding this comment

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

ditto


void defaultSourceParametersTest() throws IOException, SQLException, ClassNotFoundException {
// case1 url not contains parameters and properties not contains parameters
JdbcSource jdbcSource1 = new JdbcSource();
Copy link
Member

Choose a reason for hiding this comment

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

ditto

}

@NotNull private HashMap<String, Object> getMap() {
HashMap<String, Object> map = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
HashMap<String, Object> map = new HashMap<>();
Map<String, Object> map = new HashMap<>();

Assertions.assertEquals(connectionProperties4.get("rewriteBatchedStatements"), "false");
}

@NotNull private HashMap<String, Object> getMap() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@NotNull private HashMap<String, Object> getMap() {
@NotNull private Map<String, Object> getDefaultConfigMap() {


// case3 url not contains parameters and properties not contains parameters
JdbcSource jdbcSource3 = new JdbcSource();
HashMap<String, Object> map3 = getMap();
Copy link
Member

Choose a reason for hiding this comment

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

Please use Map not HashMap to keep code style.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Comment on lines 386 to 393
// case2 url contains parameters and properties not contains parameters
JdbcSource jdbcSource2 = new JdbcSource();
HashMap<String, Object> map2 = getMap();
map2.put("url", getUrl() + "?rewriteBatchedStatements=false");
map2.put("query", SQL);
Config config2 = ConfigFactory.parseMap(map2);
Properties connectionProperties2 = getSourceProperties(jdbcSource2, config2);
Assertions.assertEquals(connectionProperties2.get("rewriteBatchedStatements"), "true");
Copy link
Member

Choose a reason for hiding this comment

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

Why we change rewriteBatchedStatements to false in url but get true in connectionProperties?

Copy link
Member

Choose a reason for hiding this comment

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

Why we change rewriteBatchedStatements to false in url but get true in connectionProperties?

I think this test case means the param set in Properties will overwrite param set in jdbc url.

Copy link
Member

Choose a reason for hiding this comment

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

But it overwrited by default value. I think the priority should be: properties map > url > default properties. Not : properties map > default properties > url.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, you're right

Comment on lines 454 to 471
private static Object getFieldValue(Object object, String name) {
Class objClass = object.getClass();
Field[] fields = objClass.getDeclaredFields();
for (Field field : fields) {
try {
String fieldName = field.getName();
if (fieldName.equalsIgnoreCase(name)) {
field.setAccessible(true);
return field.get(object);
}
} catch (SecurityException e) {

} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return null;
}
Copy link
Member

Choose a reason for hiding this comment

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

Please use ReflectionUtils::getField to replace it.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@Hisoka-X Hisoka-X left a comment

Choose a reason for hiding this comment

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

LGTM if test passed.

Copy link
Member

@EricJoy2048 EricJoy2048 left a comment

Choose a reason for hiding this comment

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

LGTM

@EricJoy2048 EricJoy2048 merged commit d31e947 into apache:dev Oct 18, 2023
6 checks passed
@Hisoka-X
Copy link
Member

Thanks @Carl-Zhou-CN and all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants