Show Some Love
JAVA

java.sql.Connection Isolation Level

This forum was posted on Saturday, May 18, 2013 4:31:52 AM


I'm writing little spike code snippet that is not it giving me the the output I'm expecting.
I hv a table that is generally rows of counters. Other tables use these rows to generate what should be unique ID's.
When I execute the code snippet following what I excepted is that the 1st thread to reach the the select statement would acquire a lock on that row or table stopping all read or writes on the unique id value. However the second thread generally completes before the 1st one due to it been put to sleep for 1s thus they both read the similar value and write the similar value so it is only increment once and not two times as I excepted.
Is there anything wrong with my code snippet or is my understanding of the Isolation level's incorrect?
I hv removed the boiler plate code snippet. Standard sql.Connection using a MySQL DB.

private void incrementValue() {
connection
.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
statement = connection.createStatement();
System.out.println(Thread.currentThread().getName()
+ " doing select");
resultSet = statement.executeQuery("select * from counter");
System.out.println(Thread.currentThread().getName()
+ "after select");
if (counter++ == 0) {
Thread.sleep(1000);
}
String incrementedValue = getIncrementedValue(resultSet);
statement.executeUpdate("update counter set counter='"
+ incrementedValue + "'");
}
private String getIncrementedValue(ResultSet resultSet) throws SQLException {
String value = "";
if (resultSet.next()) {
System.out.println(Thread.currentThread().getName() + "Value was "
+ resultSet.getString(1));
value = (new Integer(resultSet.getString(1)) + 1) + "";
}
return value;
}

This is called from main
public static void main(String args) {
DatabaseExample databaseExample = new DatabaseExample();
Runnable runnable = new Runnable() {
@Override
public void execute() {
DatabaseExample databaseExample = new DatabaseExample();
databaseExample.incrementValue();
}
};
new Thread(runnable).start();
databaseExample.incrementValue();
}



117 Viewed this Question



 Recent Replies on "java.sql.Connection Isolation Level"

  • Nobody has replied on this post, be first to post answer.

Post Your Solution for this Question


Note: Please Put Code into <code></code> block.

Threads related to "java.sql.Connection Isolation Level":

Java SWT Update Text control

I hv written much java code snippet snippet since 1996 but never tried SWT. I though I would pick something really simple to start with but I became stuck quite early on even with the a some SWT/Jface/RCP books before me which do not Plz Please help me in this in this with my problem.I am trying to make a command line application run in an SWT window. It does not require any sort of input from the user it just needs to show sysout. The application rarely produces more than 30 lines of sysout.public class TestGui imp

Opening an Eclipse project in another Eclipse instance on another computer

I hv made a Java project with two packages using Eclipse on my desktop computer. each package has a number of files including little JUnit tests. The project root folder is located in my Dropbox folder and I'm now trying to open this project from my Laptop without access to my desktop.I hv handled to get 'Import' the project using File -> Import -> General -> Existing Projects into Workspacebut the imported project only partially resembles the one I had on my desktop.On my desktop

Collections.sort with anonymous Comparable is giving an error

first of all I hv to say that I did extensively search for an solution to this but I was incapable to found it.generally I use the ditto similar format as tons of other websites use but my NetBeans is giving an error to this.My code snippet snippet:package ocr;public class feature { public static final int METHOD_NORMAL_SEARCH = 1; public static final int METHOD_ADVANCED_SEARCH = 2; private String feature; private ArrayList names; protected HashMap values; public feature(String propertyName ArrayList n

How to detect number keys in a NativeEvent?

searching at com.google.gwt.DOM(Document Object Model)(Document Object Model).member.NativeEvent extends JavaScriptObject How can i detect if the event was a number?I thought of char.isDigit(event.getKeyCode()) but this would give me an error: The method isDigit(char) in the type char is not applicable for the arguments (int)How could I else detect his?i'm working with GWT Java for this.

Local variable not initialized catch-22

Java newguy here! i require to generate a Java method that keeps a running total of the values passed to it. However if I use a local variable and keep initializing it I will lose my running balance. do you know approach around this?

Strange Java null behavior in Method Overloading

I hv the following code snippet snippet snippet:public static void foo(Object x) { System.out.println("Obj");}public static void foo(String x) { System.out.println("Str");}If I call foo(null) why is there no ambiguity? Why does The code snippet call foo(String x) Rather of foo(Object x)?

Unable to Create Math object in Java?

I am trying to generate an object of the Math in Java. Ideally There is no require of making such an instance as it only has static methods and parameters. I just wish to generate it whether it will permit me or not. So when I am making a math class object compiler error is displayed saying that the Math class constructor is not visible. But I looked into the Math class code snippet snippet and There is no explict constructor provided so java will provide a default constructor which can be accessed

Spring cacheManager is not singleton?

I hv little issues with an spring AnnotationConfigApplicationContext.The main aim is to generate a Spring Configuration which can be run on application server or standalone. Subtask to make subcontext from this one which will be used by other application on the similar AppServer. But have little issue with cacheManager.That is my code snippet snippet:aka AbstractConfigpackage org.zib.test.a;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;import org.springfr

BinaryTree - with switch case

I played a bit with binary trees and built a menu which the user chooses whether to make a binary tree insert a value to the binary tree he built or delete it When I was clicking on the creation of a tree. The tree is made then the menu appeared again and now i require to put a number in this tree but that variable is not set in the case each case should set it's variable ? Or you can use a global variable?here it's my code snippet snippet of the menu class.import java.util.Comparator; import

Dividing Array Index by next Index Java

I am still learning Java and have a issue on an array.My array: double arr = {1.8 3.6 5.0 2.0};my problem is How do I divide the first index by the next and so on but not the last index.each index is to be divided by 2. So that the resulting array appears like this:double arr = {0.5 0.72 2.5 2.0};I am on the right track by proceeding this approach:public static void main(String args){ double arr = {1.8 3.6 5.0 2.0}; for(int j = 0; j arrj = arr0 / arr1; arrj = arr1 / arr2; System.out.prin