-
Notifications
You must be signed in to change notification settings - Fork 200
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
Add support for customizing transaction start #643
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.vertx.db2client.impl.util; | ||
|
||
import io.vertx.sqlclient.transaction.TransactionAccessMode; | ||
import io.vertx.sqlclient.transaction.TransactionIsolationLevel; | ||
|
||
public class TransactionSqlBuilder { | ||
private static final String SET_ISOLATION = "SET TRANSACTION"; | ||
|
||
private static final String PREDEFINED_TX_REPEATABLE_READ = " ISOLATION LEVEL REPEATABLE READ"; | ||
private static final String PREDEFINED_TX_SERIALIZABLE = " ISOLATION LEVEL SERIALIZABLE"; | ||
private static final String PREDEFINED_TX_READ_COMMITTED = " ISOLATION LEVEL READ COMMITTED"; | ||
private static final String PREDEFINED_TX_READ_UNCOMMITTED = " ISOLATION LEVEL READ UNCOMMITTED"; | ||
|
||
|
||
private static final String PREDEFINED_TX_RW = " READ WRITE"; | ||
private static final String PREDEFINED_TX_RO = " READ ONLY"; | ||
|
||
public static String buildSetTxIsolationLevelSql(TransactionIsolationLevel isolationLevel, TransactionAccessMode accessMode) { | ||
boolean isCharacteristicExisted = false; | ||
StringBuilder sqlBuilder = new StringBuilder(SET_ISOLATION); | ||
|
||
if (isolationLevel != null) { | ||
switch (isolationLevel) { | ||
case READ_UNCOMMITTED: | ||
sqlBuilder.append(PREDEFINED_TX_READ_UNCOMMITTED); | ||
break; | ||
case READ_COMMITTED: | ||
sqlBuilder.append(PREDEFINED_TX_READ_COMMITTED); | ||
break; | ||
case REPEATABLE_READ: | ||
sqlBuilder.append(PREDEFINED_TX_REPEATABLE_READ); | ||
break; | ||
case SERIALIZABLE: | ||
sqlBuilder.append(PREDEFINED_TX_SERIALIZABLE); | ||
break; | ||
} | ||
isCharacteristicExisted = true; | ||
} | ||
|
||
if (accessMode != null) { | ||
if (isCharacteristicExisted) { | ||
sqlBuilder.append(','); | ||
} else { | ||
isCharacteristicExisted = true; | ||
} | ||
switch (accessMode) { | ||
case READ_ONLY: | ||
sqlBuilder.append(PREDEFINED_TX_RO); | ||
break; | ||
case READ_WRITE: | ||
sqlBuilder.append(PREDEFINED_TX_RW); | ||
break; | ||
} | ||
} | ||
|
||
return sqlBuilder.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.vertx.db2client.util; | ||
|
||
import io.vertx.db2client.impl.util.TransactionSqlBuilder; | ||
import io.vertx.sqlclient.transaction.TransactionAccessMode; | ||
import io.vertx.sqlclient.transaction.TransactionIsolationLevel; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TransactionSqlBuilderTest { | ||
@Test | ||
public void testSetReadCommitted() { | ||
String sql = TransactionSqlBuilder.buildSetTxIsolationLevelSql(TransactionIsolationLevel.READ_COMMITTED, null); | ||
Assert.assertEquals("SET TRANSACTION ISOLATION LEVEL READ COMMITTED" ,sql); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on some quick experiments I ran locally this is not supported by DB2. Instead I think we need to use syntax like this: Before we add code like this, it would be good to have some more functional tests in place |
||
} | ||
|
||
@Test | ||
public void testSetReadOnly() { | ||
String sql = TransactionSqlBuilder.buildSetTxIsolationLevelSql(null, TransactionAccessMode.READ_ONLY); | ||
Assert.assertEquals("SET TRANSACTION READ ONLY" ,sql); | ||
} | ||
|
||
@Test | ||
public void testSerializableReadOnly() { | ||
String sql = TransactionSqlBuilder.buildSetTxIsolationLevelSql(TransactionIsolationLevel.SERIALIZABLE, TransactionAccessMode.READ_ONLY); | ||
Assert.assertEquals("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, READ ONLY" ,sql); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aguibert I failed to make these tests pass, can you help with this? I tried to set transaction characteristic but I got this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DB2 doesn't support setting a transaction as read only via SQL. Instead, attributes like read-only and isolation level are typically specified as "query attributes" which can be appended to the SQL. For example: