Friday, November 23, 2007

SAVEPOINT Problem

Have you used the Database keyword "SAVEPOINT" before? Have you used it within Java?
First of all, I would like to give a brief definition and description for "SAVEPOINT".

"A savepoint is a way of implementing subtransactions (also known as nested transactions) within a relational database management system by indicating a point within a transaction that can be "rolled back to" without affecting any work done in the transaction before the savepoint was created. Multiple savepoints can exist within a single transaction. Savepoints are useful for implementing complex error recovery in database applications — if an error occurs in the midst of a multiple-statement transaction, the application may be able to recover from the error (by rolling back to a savepoint) without needing to abort the entire transaction.

A savepoint can be declared by issuing a SAVEPOINT name statement. All changes made after a savepoint has been declared can be undone by issuing a ROLLBACK TO SAVEPOINT name command. Issuing RELEASE SAVEPOINT name will cause the named savepoint to be discarded, but will not otherwise affect anything. Issuing the commands ROLLBACK or COMMIT will also discard any savepoints created since the start of the main transaction.

Savepoints are supported in some form or other in database systems like PostgreSQL, Oracle, Microsoft SQL Server, MySQL, DB2, and Firebird. Savepoints are also defined in the SQL standard."

This definition is quoted from wikipedia.


The first time I tried SAVEPOINT on Oracle 9i Database using JDK 1.4 - namely JDK 1.4.2_04 on JDevelope 10g- was about 2 years ago. At that time, I have been encountered with a problem when I tried to use it calling the method setSavePoint() in the java.sql.Connection interface. I also tried it, then, using JDK 5 but do not remember the exact version and faced the same problem. The problem was that I had the run time exception "java.lang.AbstractMethodError" and from the JDK documentation I understood that this can happen in run time only if a definition of some class has incompatibly changed since the currently executing method was last compiled.

I then have posted that issue on SUN forums, but unfortunately had no reply with a solution. You can find this post on the following link
Savepoint Issue
And this is a sample code similar to my own:
String s = "jdbc:oracle:thin:@" + strHostName + ":" + strPortNo + ":" + strDBName;
Class.forName(strDriverName);
Connection connection = DriverManager.getConnection(s,strUserName,strPassword);
connection.setAutoCommit(false);
.
.
.
Savepoint save =connection.setSavepoint();
.
.
.


Anyway, the only solution I have done was a little workaround. I just used the "SAVEPOINT" of Oracle Engine itself. All what I have done was something like the follwoing:
Class.forName(strDriverName);
Connection connection = DriverManager.getConnection(s,strUserName,strPassword);
connection.setAutoCommit(false);
.
.
.
Statement stmt = connection.createStatement();
for(int i=o; i< someArray.length ; i++)
{
.
.
stmt.execute("SAVEPOINT point"+i);
.
.
}

And then when I wanted to rollback to a specific savepoint I wrote code similar to

stmt.execute("ROLLBACK to point"+2);
This can rollback to the savepoint named "point2".


Cooool. It worked just fine for me.

To be frank, I have not tried to work with SAVEPOINT again lately, maybe because I did not encounter a business case that let me try it again. Also I have never tried it with JDK 6 or any other Oracle JDBC Driver, which I think was the main reason for the problem. But I am eager to know if anyone has recently worked with it, on what JDK version, and whether it has worked well from within the Java code and invocation for the setSavePoint() method of the java.sql.Connection interface and with what Oracle version and Oracle JDBC Driver version.

Waiting for your comments and experience conerning this matter.

Monday, November 19, 2007

Java Enhanced For Loops

Today, I am going to introduce one of the Java Technical Tips that I have previously received from sun on my e-mail. I wanted to share it with you . I hope every one who reads this tip will get benefit from.

The author of this tip is John Zukowski, president and principal consultant of JZ Ventures, Inc.
This tip was developed using Java SE 6.


Using Enhanced For-Loops with Your Classes

The enhanced for-loop is a popular feature introduced with the Java SE platform in version 5.0. Its simple structure allows one to simplify code by presenting for-loops that visit each element of an array/collection without explicitly expressing how one goes from element to element.

Because the old style of coding didn't become invalid with the new for-loop syntax, you don't have to use an enhanced for-loop when visiting each element of an array/collection. However, with the new style, one's code would typically change from something like the following:

for (int i=0; i
System.out.println("Element: " + array[i]);
}

to the newer form:

for (String element : array) {
System.out.println("Element: " + element);
}

Assuming "array" is defined to be an array of String objects, each element is assigned to the element variable as it loops through the array. These basics of the enhanced for-loop were covered in an earlier Tech Tip: The Enhanced For Loop, from May 5, 2005.

If you have a class called Colony which contains a group of Penguin objects, without doing anything extra to get the enhanced for-loop to work, one way you would loop through each penguin element would be to return an Iterator and iterate through the colony. Unfortunately, the enhanced for-loop does not work with Iterator , so the following won't even compile:


// Does not compile
import java.util.*;
public class BadColony {
static class Penguin {
String name;
Penguin(String name) {
this.name = name;
}
public String toString() {
return "Penguin{" + name + "}";
}
}

Set set = new HashSet();

public void addPenguin(Penguin p) {
set.add(p);
}

public Iterator getPenguins() {
return set.iterator();
}

public static void main(String args[]) {
Colony colony = new Colony();
Penguin opus = new Penguin("Opus");
Penguin chilly = new Penguin("Chilly Willy");
Penguin mumble = new Penguin("Mumble");
Penguin emperor = new Penguin("Emperor");
colony.addPenguin(opus);
colony.addPenguin(chilly);
colony.addPenguin(mumble);
colony.addPenguin(emperor);
Iterator it = colony.getPenguins();
// The bad line of code:
for (Penguin p : it) {
System.out.println(p);
}
}
}

You cannot just pass an Iterator into the enhanced for-loop. The 2nd line of the following will generate a compilation error:

Iterator it = colony.getPenguins();
for (Penguin p : it) {

The error:

BadColony.java:36: foreach not applicable to expression type
for (Penguin p : it) {
^
1 error

In order to be able to use your class with an enhanced for-loop, it does need an Iterator , but that Iterator must be provided via the Iterable interface:


public interface java.lang.Iterable {
public java.util.Iterator iterator();
}

Actually, to be more correct, you can use a generic T , allowing the enhanced for-loop to avoid casting, returning the designated generic type, instead of just a plain old Object .

public interface java.lang.Iterable {
public java.util.Iterator iterator();
}

It is this Iterable object which is then provided to the enhanced for-loop. By making the Colony class implement Iterable , and having its new iterator()Iterator that getPenguins() provides, you'll be able to loop through the penguins in the colony via an enhanced for-loop. method return the

By adding the proper implements clause:

public class Colony implements Iterable {


You then get your enhanced for
-loop for the colony:

for (Penguin p : colony) {

Here's the updated Colony class with the corrected code:

import java.util.*;

public class Colony implements Iterable {

static class Penguin {
String name;
Penguin(String name) {
this.name = name;
}
public String toString() {
return "Penguin{" + name + "}";
}
}

Set set = new HashSet();

public void addPenguin(Penguin p) {
set.add(p);
}

public Iterator getPenguins() {
return set.iterator();
}

public Iterator iterator() {
return getPenguins();
}

public static void main(String args[]) {
Colony colony = new Colony();
Penguin opus = new Penguin("Opus");
Penguin chilly = new Penguin("Chilly Willy");
Penguin mumble = new Penguin("Mumble");
Penguin emperor = new Penguin("Emperor");
colony.addPenguin(opus);
colony.addPenguin(chilly);
colony.addPenguin(mumble);
colony.addPenguin(emperor);
for (Penguin p : colony) {
System.out.println(p);
}
}
}

Running the code produces the
following output:

> java Colony

Penguin{Chilly Willy}
Penguin{Mumble}
Penguin{Opus}
Penguin{Emperor}

Keep in mind that the individual penguins are internally kept in

a Set type collection so the returned order doesn't necessarily match

the insertion order, which in this case it doesn't.

Remember to genericize the implements clause for the class

"implements Iterable " and not just say "implements Iterable ".

With the latter, the enhanced for-loop will only return an Object for

each element.

For more information on the enhanced for-loop, please see the

Java Programming Language guide from JDK 1.5.

Saturday, November 17, 2007

ThreadLocal

Writing thread-safe classes is difficult. It requires a careful analysis of not only the conditions under which variables will be read or written, but also of how the class might be used by other classes. Sometimes, it is very difficult to make a class thread-safe without compromising its functionality, ease of use, or performance. Some classes retain state information from one method invocation to the next, and it is difficult to make such classes thread-safe in any practical way.

It may be easier to manage the use of a non-thread-safe class than to try and make the class thread-safe. A class that is not thread-safe can often be used safely in a multithreaded program as long as you ensure that instances of that class used by one thread are not used by other threads. For example, the JDBC Connection class is not thread-safe -- two threads cannot safely share a Connection at a fine level of granularity -- but if each thread had its own Connection, then multiple threads can safely perform database operations simultaneously.

It is certainly possible to maintain a separate JDBC connection (or any other object) for each thread without the use of ThreadLocal; the Thread API gives us all the tools we need to associate objects with threads. However, the ThreadLocal class makes it much easier for us to manage the process of associating a thread with its per-thread data.

Read More

Thursday, November 8, 2007

Join the Interoperability Chat — LIVE November 28

Find out how to tighten integration of your enterprise systems with new interoperability between Sun and Microsoft technologies at this live Web chat with top Sun engineers.

It's a great opportunity to explore results from three years of engineering partnership between Sun and Microsoft. Plus — learn about Sun/Intel collaboration and new Sun x64 systems with Intel Xeon CPUs.

Q&A Chat on Sun/Microsoft Interoperability
Wednesday, November 28
1530 London
1630 Paris
1730 Athens
» Check time zones

Register for chat

Tuesday, January 16, 2007

My First Blog

My first blog, for those who have not seen it before, was not actually technical. It was mainly about an internet service that I felt we can benefit from, but actually I did not get any benefit from. Maybe I used the service in a wrong way or something. Anyway, execuse me that I am modifying my first blog now trying to make it useful for any technical person who is interested in java development.

I will start this time with some latest news about Sun Microsystems. This is mainly important for the Arab and Middle East people. My main source of this news is an email from the EGJUG (Egypt Java Users Group) leader, Ahmed Hashim.


"Sun Microsystems is going to make a set of technical days in the Middle East, starting with Dubai-UAE then Cairo-Egypt.

They will make a technical day in Dubai on 19 November 2007 about Netbeans IDE ,Glassfish application server and J2ME. Check the website for more information
http://www.cpilive.net/nl/2007/uadf/index.aspx"

Hope as many Arabs and Middle Easterns benefit as much as possible from such an event.