Skip to content

Commit aa2f77c

Browse files
committed
Add TRANSACTION_ISOLATION_LEVEL config in PostgreSQL plugins
1 parent cde7167 commit aa2f77c

21 files changed

+192
-16
lines changed

database-commons/src/main/java/io/cdap/plugin/db/ConnectionConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public abstract class ConnectionConfig extends PluginConfig implements DatabaseC
4545
public static final String CONNECTION_ARGUMENTS = "connectionArguments";
4646
public static final String JDBC_PLUGIN_NAME = "jdbcPluginName";
4747
public static final String JDBC_PLUGIN_TYPE = "jdbc";
48+
public static final String TRANSACTION_ISOLATION_LEVEL = "transactionIsolationLevel";
49+
4850

4951
@Name(JDBC_PLUGIN_NAME)
5052
@Description("Name of the JDBC driver to use. This is the value of the 'jdbcPluginName' key defined in the JSON " +

database-commons/src/main/java/io/cdap/plugin/db/config/AbstractDBSpecificSourceConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import io.cdap.plugin.db.TransactionIsolationLevel;
2929
import io.cdap.plugin.db.connector.AbstractDBConnectorConfig;
3030
import io.cdap.plugin.db.source.AbstractDBSource;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
3133

3234
import java.io.IOException;
3335
import java.util.Collections;
@@ -49,6 +51,7 @@ public abstract class AbstractDBSpecificSourceConfig extends PluginConfig implem
4951
public static final String DATABASE = "database";
5052
public static final String FETCH_SIZE = "fetchSize";
5153
public static final String DEFAULT_FETCH_SIZE = "1000";
54+
public static final Logger LOG = LoggerFactory.getLogger(AbstractDBSpecificSourceConfig.class);
5255

5356
@Name(Constants.Reference.REFERENCE_NAME)
5457
@Description(Constants.Reference.REFERENCE_NAME_DESCRIPTION)

database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBConnectorConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import io.cdap.plugin.common.KeyValueListParser;
2626
import io.cdap.plugin.common.db.DBConnectorProperties;
2727
import io.cdap.plugin.db.ConnectionConfig;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
2830

2931
import java.util.Collections;
3032
import java.util.HashMap;
@@ -37,6 +39,8 @@
3739
*/
3840
public abstract class AbstractDBConnectorConfig extends PluginConfig implements DBConnectorProperties {
3941

42+
private static final Logger LOG = LoggerFactory.getLogger(AbstractDBConnectorConfig.class);
43+
4044
@Name(ConnectionConfig.JDBC_PLUGIN_NAME)
4145
@Description("Name of the JDBC driver to use. This is the value of the 'jdbcPluginName' key defined in the JSON " +
4246
"file for the JDBC plugin.")

database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBSpecificConnector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.apache.hadoop.mapreduce.MRJobConfig;
3838
import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
3939
import org.apache.hadoop.mapreduce.lib.db.DBWritable;
40+
import org.slf4j.Logger;
41+
import org.slf4j.LoggerFactory;
4042

4143
import java.io.IOException;
4244
import java.sql.Connection;
@@ -56,6 +58,7 @@ public abstract class AbstractDBSpecificConnector<T extends DBWritable> extends
5658
implements BatchConnector<LongWritable, T> {
5759

5860
private final AbstractDBConnectorConfig config;
61+
private static final Logger LOG = LoggerFactory.getLogger(AbstractDBSpecificConnector.class);
5962

6063
protected AbstractDBSpecificConnector(AbstractDBConnectorConfig config) {
6164
super(config);
@@ -99,6 +102,7 @@ public InputFormatProvider getInputFormatProvider(ConnectorContext context, Samp
99102
tableQuery, null, false);
100103
connectionConfigAccessor.setConnectionArguments(Maps.fromProperties(config.getConnectionArgumentsProperties()));
101104
connectionConfigAccessor.getConfiguration().setInt(MRJobConfig.NUM_MAPS, 1);
105+
LOG.debug("Moving inside AbstractDBConnectorConfig");
102106
Map<String, String> additionalArguments = config.getAdditionalArguments();
103107
for (Map.Entry<String, String> argument : additionalArguments.entrySet()) {
104108
connectionConfigAccessor.getConfiguration().set(argument.getKey(), argument.getValue());

database-commons/src/main/java/io/cdap/plugin/db/connector/AbstractDBSpecificConnectorConfig.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import io.cdap.cdap.api.annotation.Macro;
2121
import io.cdap.cdap.api.annotation.Name;
2222
import io.cdap.plugin.db.ConnectionConfig;
23+
import io.cdap.plugin.db.TransactionIsolationLevel;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2326

2427
import java.util.Collections;
28+
import java.util.HashMap;
2529
import java.util.Map;
2630
import javax.annotation.Nullable;
2731

@@ -30,9 +34,11 @@
3034
*/
3135
public abstract class AbstractDBSpecificConnectorConfig extends AbstractDBConnectorConfig {
3236

37+
private static final Logger LOG = LoggerFactory.getLogger(AbstractDBSpecificConnectorConfig.class);
3338
@Name(ConnectionConfig.HOST)
3439
@Description("Database host")
3540
@Macro
41+
3642
@Nullable
3743
protected String host;
3844

@@ -42,6 +48,12 @@ public abstract class AbstractDBSpecificConnectorConfig extends AbstractDBConnec
4248
@Nullable
4349
protected Integer port;
4450

51+
@Name(ConnectionConfig.TRANSACTION_ISOLATION_LEVEL)
52+
@Description("The transaction isolation level for the database session.")
53+
@Macro
54+
@Nullable
55+
protected String transactionIsolationLevel;
56+
4557
public String getHost() {
4658
return host;
4759
}
@@ -55,4 +67,22 @@ public int getPort() {
5567
public boolean canConnect() {
5668
return super.canConnect() && !containsMacro(ConnectionConfig.HOST) && !containsMacro(ConnectionConfig.PORT);
5769
}
70+
71+
@Override
72+
public Map<String, String> getAdditionalArguments() {
73+
Map<String, String> additonalArguments = new HashMap<>();
74+
LOG.debug("inside get AdditionalArguemnts of AbstractDBSpecificConnectorConfig");
75+
if (getTransactionIsolationLevel() != null) {
76+
additonalArguments.put(TransactionIsolationLevel.CONF_KEY, getTransactionIsolationLevel());
77+
}
78+
return additonalArguments;
79+
}
80+
81+
public String getTransactionIsolationLevel() {
82+
if (transactionIsolationLevel == null) {
83+
return null;
84+
}
85+
return TransactionIsolationLevel.Level.valueOf(transactionIsolationLevel).name();
86+
}
5887
}
88+

mysql-plugin/docs/MySQL-connector.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ authentication. Optional for databases that do not require authentication.
2222

2323
**Password:** Password to use to connect to the specified database.
2424

25+
**Transaction Isolation Level** The transaction isolation level of the databse connection
26+
- TRANSACTION_READ_COMMITTED: No dirty reads. Non-repeatable reads and phantom reads are possible.
27+
- TRANSACTION_SERIALIZABLE: No dirty reads. Non-repeatable and phantom reads are prevented.
28+
- TRANSACTION_REPEATABLE_READ: No dirty reads. Prevents non-repeatable reads, but phantom reads are still possible.
29+
- TRANSACTION_READ_UNCOMMITTED: Allows dirty reads (reading uncommitted changes from other transactions). Non-repeatable reads and phantom reads are possible.
30+
2531
**Connection Arguments:** A list of arbitrary string tag/value pairs as connection arguments. These arguments
2632
will be passed to the JDBC driver, as connection arguments, for JDBC drivers that may need additional configurations.
2733
This is a semicolon-separated list of key-value pairs, where each pair is separated by a equals '=' and specifies

mysql-plugin/docs/Mysql-batchsink.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ You also can use the macro function ${conn(connection-name)}.
3939

4040
**Password:** Password to use to connect to the specified database.
4141

42+
**Transaction Isolation Level** The transaction isolation level of the databse connection
43+
- TRANSACTION_READ_COMMITTED: No dirty reads. Non-repeatable reads and phantom reads are possible.
44+
- TRANSACTION_SERIALIZABLE: No dirty reads. Non-repeatable and phantom reads are prevented.
45+
- TRANSACTION_REPEATABLE_READ: No dirty reads. Prevents non-repeatable reads, but phantom reads are still possible.
46+
- TRANSACTION_READ_UNCOMMITTED: Allows dirty reads (reading uncommitted changes from other transactions). Non-repeatable reads and phantom reads are possible.
47+
4248
**Connection Arguments:** A list of arbitrary string key/value pairs as connection arguments. These arguments
4349
will be passed to the JDBC driver as connection arguments for JDBC drivers that may need additional configurations.
4450

mysql-plugin/docs/Mysql-batchsource.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ For example, 'SELECT MIN(id),MAX(id) FROM table'. Not required if numSplits is s
4949

5050
**Password:** Password to use to connect to the specified database.
5151

52+
**Transaction Isolation Level** The transaction isolation level of the databse connection
53+
- TRANSACTION_READ_COMMITTED: No dirty reads. Non-repeatable reads and phantom reads are possible.
54+
- TRANSACTION_SERIALIZABLE: No dirty reads. Non-repeatable and phantom reads are prevented.
55+
- TRANSACTION_REPEATABLE_READ: No dirty reads. Prevents non-repeatable reads, but phantom reads are still possible.
56+
- TRANSACTION_READ_UNCOMMITTED: Allows dirty reads (reading uncommitted changes from other transactions). Non-repeatable reads and phantom reads are possible.
57+
5258
**Connection Arguments:** A list of arbitrary string key/value pairs as connection arguments. These arguments
5359
will be passed to the JDBC driver as connection arguments for JDBC drivers that may need additional configurations.
5460

mysql-plugin/src/main/java/io/cdap/plugin/mysql/MysqlSource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,15 @@ public MysqlConnectorConfig getConnection() {
197197
return connection;
198198
}
199199

200+
@Override
201+
public String getTransactionIsolationLevel() {
202+
return connection.getTransactionIsolationLevel();
203+
}
204+
200205
@Override
201206
public void validate(FailureCollector collector) {
202207
ConfigUtil.validateConnection(this, useConnection, connection, collector);
208+
connection.getAdditionalArguments();
203209
super.validate(collector);
204210
}
205211

mysql-plugin/widgets/MySQL-connector.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@
3030
"widget-attributes": {
3131
"default": "3306"
3232
}
33+
},
34+
{
35+
"widget-type": "select",
36+
"label": "Transaction Isolation Level",
37+
"name": "transactionIsolationLevel",
38+
"widget-attributes": {
39+
"values": [
40+
"TRANSACTION_READ_UNCOMMITTED",
41+
"TRANSACTION_READ_COMMITTED",
42+
"TRANSACTION_REPEATABLE_READ",
43+
"TRANSACTION_SERIALIZABLE"
44+
],
45+
"default": "TRANSACTION_SERIALIZABLE"
46+
}
3347
}
3448
]
3549
},

0 commit comments

Comments
 (0)