<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-20547117</id><updated>2012-02-16T13:09:49.196Z</updated><title type='text'>A Programmer's Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>31</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-20547117.post-2300522095598722352</id><published>2010-05-23T01:38:00.003+01:00</published><updated>2010-07-10T01:11:17.283+01:00</updated><title type='text'>Java versus Google Go - Part 2</title><content type='html'>The new &lt;a href="http://www.golang.org/"&gt;Go Programming language&lt;/a&gt; from Google is very interesting because it attempts to bring to the world of compiled languages some of the benefits of the VM based languages, such as garbage collection and dynamic interfaces. I am considering porting one of my projects to Go, but before diving in, I would like to explore Go by writing a few small programs and comparing these with the Java versions.&lt;br /&gt;&lt;br /&gt;Without further ado, here is a very simple program that reads a file and outputs lines to the console. First, lets look at the Java version:&lt;br /&gt;&lt;pre&gt;package org.majumdar;&lt;br /&gt;&lt;br /&gt;import java.io.BufferedReader;&lt;br /&gt;import java.io.FileReader;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.Reader;&lt;br /&gt;&lt;br /&gt;public class CatFile {&lt;br /&gt;&lt;br /&gt;        public static void main(String[] args) {&lt;br /&gt;                if (args.length == 0) {&lt;br /&gt;                        usage();&lt;br /&gt;                        return;&lt;br /&gt;                }&lt;br /&gt;                BufferedReader reader = null;&lt;br /&gt;                try {&lt;br /&gt;                        reader = new BufferedReader(new FileReader(args[0]));&lt;br /&gt;                        String line;&lt;br /&gt;                        while ((line = reader.readLine()) != null) {&lt;br /&gt;                                System.out.println(line);&lt;br /&gt;                        }&lt;br /&gt;                } catch (Exception e) {&lt;br /&gt;                        System.err.println("error: " + e.getMessage());&lt;br /&gt;                } finally {&lt;br /&gt;                        close(reader);&lt;br /&gt;                }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static void close(Reader reader) {&lt;br /&gt;                if (reader == null)&lt;br /&gt;                        return;&lt;br /&gt;                try {&lt;br /&gt;                        reader.close();&lt;br /&gt;                } catch (IOException e) {&lt;br /&gt;                }&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        private static void usage() {&lt;br /&gt;                System.out.println("usage: CatFile ");&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, the same program implemented in Go:&lt;br /&gt;&lt;pre&gt;package main&lt;br /&gt;&lt;br /&gt;import "fmt"&lt;br /&gt;import "os"&lt;br /&gt;import "bufio"&lt;br /&gt;&lt;br /&gt;func usage() {&lt;br /&gt;        fmt.Printf("usage: catfile &lt;filename&gt;\n")&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;func main() {&lt;br /&gt;        if len(os.Args) &amp;lt; 2 {&lt;br /&gt;                usage()&lt;br /&gt;                return&lt;br /&gt;        }&lt;br /&gt;        f, err := os.Open(os.Args[1], os.O_RDONLY, 0)&lt;br /&gt;        if err != nil {&lt;br /&gt;                fmt.Printf("error: %s\n", err)&lt;br /&gt;                return&lt;br /&gt;        }&lt;br /&gt;        defer f.Close()&lt;br /&gt;        r := bufio.NewReader(f);&lt;br /&gt;        for {&lt;br /&gt;                line, err := r.ReadString('\n');&lt;br /&gt;                if err == os.EOF {&lt;br /&gt;                        break&lt;br /&gt;                }&lt;br /&gt;                if err != nil {&lt;br /&gt;                        fmt.Printf("error: %s\n", err)&lt;br /&gt;                        break&lt;br /&gt;                }&lt;br /&gt;                fmt.Printf("%s", line);&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;&lt;/filename&gt;&lt;/pre&gt;I am really not sure which one of the two is more readable.&lt;br /&gt;&lt;br /&gt;The main differences in the two programs are in how errors are handled, and how resources are cleaned up.&lt;br /&gt;&lt;br /&gt;Java offers the finally clause in a try block for cleaning up resources; the Go approach is to allow functions to be scheduled to be invoked when the enclosing function returns via the defer statement. The Go approach doesn't offer much programmer control over when the cleanup should occur. With a try block, the placement of the cleanup code is more under the programmer's control.&lt;br /&gt;&lt;br /&gt;Error handling in Java is based upon exception management. Go doesn't have exception management yet; although some form of exception management is planned. The authors of Go seem opposed to exception handling as a mechanism for error handling; their argument is that the try-catch-finally construct makes the code convoluted and that encourages programmers to label ordinary errors as exceptions. My personal preference is for the Java approach because it forces you to handle the error condition. By convention in Java (although the language does not enforce this), error conditions are indicated via exceptions and not by return values.&lt;br /&gt;&lt;br /&gt;I think with either approach you can write bad code that doesn't handle errors properly. In Java, you can do this by handling the exception incorrectly; in Go, if you forget to check for an error condition, the program will probably fail at runtime in an unexpected way.&lt;br /&gt;&lt;br /&gt;My initial thoughts are that I prefer the try-catch-finally approach to the Go approach, both for error handling and for resource cleanup. Of course the Java approach isn't perfect; for example, the usefulness of checked exceptions is doubtful, and there could be better support for resource cleanup - in fact this is coming in &lt;a href="http://openjdk.java.net/projects/jdk7/features/#f618"&gt;Java 7&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The programs listed above are trivial, and the comparison is not really fair as the strengths and weaknesses of the two languages are not clear. I am hoping to compare two additional programs - a simple TCP/IP server implementation, and a Lock Scheduler implementation. I have the Java versions of these, and am hoping to write the Go versions in the next few days.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-2300522095598722352?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/2300522095598722352/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=2300522095598722352' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/2300522095598722352'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/2300522095598722352'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2010/05/google-go-versus-java.html' title='Java versus Google Go - Part 2'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-8276119479106788152</id><published>2010-04-02T02:08:00.000+01:00</published><updated>2010-04-02T02:08:31.689+01:00</updated><title type='text'>Testing concurrent programs</title><content type='html'>Testing concurrent programs is particularly hard, as the interleaving of multiple threads of execution greatly multiples the number of possible code and data access paths. It is quite challenging to write test cases that properly test such scenarios, usually only a handful can be tested.&lt;br /&gt;&lt;br /&gt;A unique tool that helps with testing for concurrency bugs is an IBM product named &lt;a href="http://www.alphaworks.ibm.com/tech/contest"&gt;ConTest&lt;/a&gt;. ConTest does not generate any new test cases, but if you already have multi-threaded test scenarios, it increases the likelihood of bugs being triggered by introducing random pauses in thread execution.&lt;br /&gt;&lt;br /&gt;A while ago, I tried running ConTest against my test suite for SimpleDBM; I found that execution had slowed considerably. So expect your test cases to take much longer to complete.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-8276119479106788152?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/8276119479106788152/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=8276119479106788152' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/8276119479106788152'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/8276119479106788152'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2010/04/testing-concurrent-programs.html' title='Testing concurrent programs'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-4393120550653926603</id><published>2010-03-28T23:50:00.000+01:00</published><updated>2010-03-28T23:50:57.484+01:00</updated><title type='text'>Global Loggers</title><content type='html'>It seems that all existing logging libraries assume that you want a single global Log Manager, tied to the class loader. Only static methods are provided to access the Log Manager or Logger instances.&lt;br /&gt;&lt;br /&gt;I have been rigorously removing all static objects from SimpleDBM, so that the entire object graph of SimpleDBM is rooted in the main Server object. Doing this not only makes the code more robust, it also allows multiple instances of SimpleDBM to coexist in the same classloader without conflict. But where this model has broke down is in the Logger implementation, which is a wrapper for either Log4J or JDK Logging, and neither of these allow non global instances of the Log Manager.&lt;br /&gt;&lt;br /&gt;Much as I would loathe to do this, it seems the only solution is to roll out my own ...&lt;br /&gt;&lt;br /&gt;Is anyone else facing this issue?&amp;nbsp;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-4393120550653926603?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/4393120550653926603/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=4393120550653926603' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/4393120550653926603'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/4393120550653926603'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2010/03/global-loggers.html' title='Global Loggers'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-9010460267526866257</id><published>2010-03-28T16:59:00.000+01:00</published><updated>2010-03-28T16:59:55.074+01:00</updated><title type='text'>Life is too short</title><content type='html'>Life is too short to be able to master multiple programming languages and tools. So while I would love to learn the new &lt;a href="http://golang.org/"&gt;Go Programming Language&lt;/a&gt;, Python, AJAX, and a few other cool new things, I keep going back to what I already know, the Java programming language. I can write a small utility faster in Java than if I wrote the same utility in Python; not because Java is particularly productive, but because I do not have to spend time figuring out how to do something in Java.&lt;br /&gt;&lt;br /&gt;This is where I think something like the &lt;a href="http://code.google.com/webtoolkit/"&gt;Google Web Toolkit&lt;/a&gt; is a Godsend for someone like me. Being able to create a web user interface in Java, without having to know the intricacies of&amp;nbsp; AJAX, JSP, Java Server Faces, etc. is way too cool. Of course, there is a learning curve here too, but it is a less steeper curve because the language is already familiar.&lt;br /&gt;&lt;br /&gt;I am using GWT to create a small application to demonstrate the use of &lt;a href="http://simpledbm.googlecode.com/"&gt;SimpleDBM&lt;/a&gt;. I am just loving the experience. Kudos to the Google team for coming out with GWT!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-9010460267526866257?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/9010460267526866257/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=9010460267526866257' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/9010460267526866257'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/9010460267526866257'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2010/03/life-is-too-short.html' title='Life is too short'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-6478946283089356902</id><published>2010-03-21T23:42:00.001Z</published><updated>2010-03-21T23:52:39.128Z</updated><title type='text'>Java versus Google Go</title><content type='html'>An interesting new systems programming language was announced by Google at the end of last year - The Go Programming Language (&lt;a href="http://www.golang.org/"&gt;www.golang.org&lt;/a&gt;). Is this what Java should have been?&lt;br /&gt;&lt;br /&gt;Go is of course new and old. It is a new language that derives a lot from the past work done by its creators at Bell Labs. You can even see copyright notices from Plan 9 etc. all over the place. Therefore although the language is new, it is built upon years of experience.&lt;br /&gt;&lt;br /&gt;In general I like the new language. Two features are particularly nice:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Any object can be cast to an interface as long as the object implements the signature of the interface (sorry for using Java terminology here).&lt;/li&gt;&lt;li&gt;Go routines are cool as it overcomes the problem that equating a thread with a process flow creates. In other languages, if your thread blocks, your program halts. In Go, a routine that is blocked is moved out and some other routine takes it place on the thread. This will be very good for servers that need the ability to multiplex processing over a limited set of threads.&lt;/li&gt;&lt;/ul&gt;There are a few ugly things too. My dislikes are the built in allocation functions new() and make() and the &amp;nbsp;pointer type. Why this mess? Java is so neat you either have primitives or references. References are like pointers except that you cannot do any pointer operations with them.&lt;br /&gt;&lt;br /&gt;The top feature that is missing is an exception handling mechanism. I have programmed many years in C and now in Java, and I can tell you that it is far easier to create robust error handling in Java. Of course, checked exceptions were a mistake (I have changed my mind about them) and I think Go should avoid them.&lt;br /&gt;&lt;br /&gt;I wish I had the luxury of rewriting SimpleDBM in Go. It would be an interesting and fun thing to do. But I have better things to do... &amp;nbsp;I am hoping that I can at least create a network client in Go, so that it is possible to talk to the SimpleDBM server from Go.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-6478946283089356902?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/6478946283089356902/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=6478946283089356902' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/6478946283089356902'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/6478946283089356902'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2010/03/java-versus-google-go.html' title='Java versus Google Go'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-1240158548813921689</id><published>2007-12-04T22:44:00.000Z</published><updated>2007-12-04T23:00:55.700Z</updated><title type='text'>Java on Mac OS X</title><content type='html'>I posted &lt;a href="http://simpledbm.blogspot.com/2007/09/java-on-mac.html"&gt;elsewhere&lt;/a&gt; that I now own an iMac and am doing most of my Java development on it. The big problem with Java on Mac is that the JDK is pretty out of date. The official version that comes with Mac OS X 10.4 Tiger is 1.5.0_07. This version seems buggy, as the JVM seems to hang when running test cases for &lt;a href="http://www.simpledbm.org/"&gt;one&lt;/a&gt; of my projects. I was getting quite frustrated, and in the end decided to run my test cases under OpenSolaris, which I installed as a VM using VMWare Fusion.&lt;br /&gt;&lt;br /&gt;Fortunately, Apple has recently made available a pre-release version 1.5.0_13 from its &lt;a href="http://developer.apple.com/"&gt;Developer Connection&lt;/a&gt; web site. You need to be registered to be able to download this release. The good news is that it fixes the bug that was causing my tests to hang the JVM. I think it has something to do with the java.util.concurrency classes. Ofcourse, if you don't use these, you would not have faced the problems I was facing.&lt;br /&gt;&lt;br /&gt;The long gap between the Java releases on Mac seems unacceptable, especially since the JDK is so buggy. The other problem is the lack of support from major vendors for the Mac platform. I would have liked to install and use IBM WebSphere and Rational products on my Mac, but it isn't amongst the supported platforms. Linux has much better support now-a-days.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-1240158548813921689?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/1240158548813921689/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=1240158548813921689' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/1240158548813921689'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/1240158548813921689'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2007/12/java-on-mac-os-x.html' title='Java on Mac OS X'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-8076198874125278932</id><published>2007-12-04T22:26:00.000Z</published><updated>2007-12-04T22:34:52.447Z</updated><title type='text'>Fast forward</title><content type='html'>It has been sometime since I last blogged about general programming topics. My day job has kept me from the joys of exploring the JPA. But many other things have happened to me in the meantime, life just goes on, and there is never enough time to catch up on everything.&lt;br /&gt;&lt;br /&gt;For a start, I want to retitle this blog, by removing the Java from it. Let this just be a Programmer's Blog.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-8076198874125278932?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/8076198874125278932/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=8076198874125278932' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/8076198874125278932'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/8076198874125278932'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2007/12/fast-forward.html' title='Fast forward'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-114696034834421346</id><published>2006-05-07T01:00:00.000+01:00</published><updated>2006-11-04T22:44:43.815Z</updated><title type='text'>Creating Oracle DataSource in Glassfish</title><content type='html'>&lt;p&gt;To setup an Oracle Datasource in Glassfish, follow these steps:&lt;br /&gt;&lt;br /&gt;1. Copy the Oracle JDBC drivers to /glassfish/domains/domain1/lib/ext directory. You may need to change the directory /glassfish/domains/domain1 to match your installation of Glassfish and the domain name.&lt;/p&gt;&lt;p&gt;2. Start Glassfish.&lt;/p&gt;&lt;p&gt;3. Login to the admin interface and create a JDBC Connection Pool.&lt;br /&gt;Delete all properties, and add following properties:&lt;br /&gt;user - set this to Oracle userid&lt;br /&gt;password - set this to Oracle password&lt;br /&gt;URL - set this to the URL, example jdbc:oracle:thin:@localhost:1521:xe.&lt;br /&gt;xa-driver-does-not-support-non-tx-operations - set this to true.&lt;br /&gt;Test the connection pool using ping.&lt;/p&gt;&lt;p&gt;4. Create a JDBC DataSource using the Connection Pool.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-114696034834421346?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/114696034834421346/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=114696034834421346' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114696034834421346'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114696034834421346'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/05/creating-oracle-datasource-in.html' title='Creating Oracle DataSource in Glassfish'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-114367162220429794</id><published>2006-03-29T23:19:00.000+01:00</published><updated>2006-11-04T22:44:43.753Z</updated><title type='text'>Oracle JDeveloper as JSF Editor</title><content type='html'>After trying out various IDEs for editing Java Server Faces (JSF) files, I have finally settled on Oracle's JDeveloper as the best tool for the purpose. I had assumed that JDeveloper would insist on using ADF, but found to my pleasant surprise that it also allows you to design standard JSF pages in a visual manner. Unlike Sun Java Studio Creator which creates its own backing bean class, JDeveloper gives you full control, and lets you decide whether you want all/some/none of your UI components to be in your backing bean.&lt;br /&gt;&lt;br /&gt;The Java IDE marketplace is such that no single tool meets all your needs. I find Eclipse is good for general purpose Java programming but not much use for J2EE stuff. Netbeans doesn't work for me - I have tried to use it several times but each time I have given up in frustration because I either cannot resolve some wierd error, or cannot get the IDE to do something simple. To give you an example, I try to edit a JSF file in Netbeans, and it will show code completion for jsp tags, but not for JSF. I don't have the time to work out why.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-114367162220429794?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/114367162220429794/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=114367162220429794' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114367162220429794'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114367162220429794'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/03/oracle-jdeveloper-as-jsf-editor.html' title='Oracle JDeveloper as JSF Editor'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-114190076419069276</id><published>2006-03-09T10:33:00.000Z</published><updated>2006-11-04T22:44:43.690Z</updated><title type='text'>Servlets and dependency injection</title><content type='html'>There is a very useful &lt;a href="http://forums.java.net/jive/thread.jspa?forumID=56&amp;threadID=13439&amp;amp;messageID=86503"&gt;thread&lt;/a&gt; at the &lt;a href="http://forums.java.net/jive/forum.jspa?forumID=56&amp;start=0"&gt;Glassfish discussion forum&lt;/a&gt; on the why it is not a good idea to use dependency injection to obtain references to EJBs or EntityManagers in Servlets.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-114190076419069276?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/114190076419069276/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=114190076419069276' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114190076419069276'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114190076419069276'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/03/servlets-and-dependency-injection.html' title='Servlets and dependency injection'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-114186757008094210</id><published>2006-03-09T01:06:00.000Z</published><updated>2006-11-04T22:44:43.622Z</updated><title type='text'>Remote or Local interface?</title><content type='html'>EJB 3.0 takes away much of the pain associated with maintaining the Local and Remote interfaces for Session Beans. However, there are still some issues that you need to be aware of.&lt;br /&gt;&lt;br /&gt;By design, the EJB specification disallows the same Business interface to be marked as @Local as well as @Remote (this restriction has been added in a revision to the PFD). The reason for this is that remote interfaces have different semantics to local interfaces, and it is usually inappropriate for the same method to be exposed as a remote interface as well as local interface. The problems are two-fold:&lt;br /&gt;&lt;br /&gt;1. Remote interfaces must typically be designed to be coarse-grained.&lt;br /&gt;2. Remote interfaces do not support the pass-by-reference semantics of parameter passing as in local interfaces.&lt;br /&gt;&lt;br /&gt;Having separate Remote and Local interfaces forces designers to think about how the interfaces will be used and ensure that the design is optimised for the use case. It also reduces the chances of errors caused by incorrect semantics, such as clients relying upon ability to pass parameters by reference.&lt;br /&gt;&lt;br /&gt;A drawback to this approach is that it does not allow transparent redeployment of an EJB from a co-located environment to a distributed environment or vice-versa. While such re-deployment has its dangers, there are times when it can prove useful to have such a facility.&lt;br /&gt;&lt;br /&gt;Ideally, you should define your Session beans to have either remote or local interface. This will keep the design simple and allow you to implement Business Logic as POJOs.&lt;br /&gt;&lt;br /&gt;If you do want to endow the same Session Bean with both remote and local interfaces, then, I suggest that you use the following example as a model:&lt;br /&gt;&lt;pre&gt;public interface CustomerService {&lt;br /&gt;    &lt;br /&gt;   void createCustomer(District d, Customer c);&lt;br /&gt;   void removeCustomer(Long id);&lt;br /&gt;   List findByDistrict(District d);&lt;br /&gt;    &lt;br /&gt;   @Remote &lt;br /&gt;   public interface IRemote extends CustomerService {&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   @Local &lt;br /&gt;   public interface ILocal extends CustomerService {&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;Note that the local and remote interfaces extend a common business interface. Also note that the local and remote interfaces are nested within the business interface. I like this model because it reduces the clutter, keeps related interfaces together, and eases understanding. &lt;br /&gt;&lt;br /&gt;When using dependency injection, you can specify explicitly whether you want the remote or local interface. For example:&lt;br /&gt;&lt;pre&gt;@EJB(beanInterface=services.DistrictService.IRemote.class)&lt;br /&gt;public final void setDistrictService(DistrictService districtService) {&lt;br /&gt;   this.districtService = districtService;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;I also suggest that when specifying dependency injection, always annotate your setters rather than instance variables. This will allow you to use your beans outside the J2EE container, for example in a Spring Framework environment.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-114186757008094210?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/114186757008094210/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=114186757008094210' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114186757008094210'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114186757008094210'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/03/remote-or-local-interface.html' title='Remote or Local interface?'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-114186638362867028</id><published>2006-03-09T00:43:00.000Z</published><updated>2006-11-04T22:44:43.557Z</updated><title type='text'>EJB 3.0 Packaging - Entities</title><content type='html'>I am looking at the various EJB 3.0 packaging options and trying to determine best practice. My first recommendation is that you should create separate projects for your entities and package your entities in independent jar files. This is important for a number of reasons.&lt;br /&gt;&lt;br /&gt;Firstly, you will want to share your entities in multiple projects, so having them maintained independently makes it easier to share them across projects.&lt;br /&gt;&lt;br /&gt;Secondly, the teams that define the entities are probably going to work closely with the folks that are responsible for defining the database Entity Model.  It will be easier if these teams can work independently from other teams.&lt;br /&gt;&lt;br /&gt;Last but not least, it is not possible to define Entities in a portable manner. The main portability issues are with the way Surrogate Keys are generated using Sequence Generators. Having separate projects for your entities will allow you to create drop-in replacements for different database vendors. So you can have one set of entities for Oracle, another for Apache Derby, and so on. For an example of this, please have a look at the &lt;a href="https://ejb3demo.dev.java.net/"&gt;EJB3Demo project&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;When you create your application archives, place the Entity jar files in the lib sub-directory within the archive. This will ensure that the entity definitions are available to all your EJBs and Web applications within the application. This &lt;a href="http://weblogs.java.net/blog/ss141213/archive/2005/12/using_java_pers.html"&gt;article&lt;/a&gt; by Sahoo explains the benefits of putting your entity jar files in the lib directory.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-114186638362867028?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/114186638362867028/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=114186638362867028' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114186638362867028'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114186638362867028'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/03/ejb-30-packaging-entities.html' title='EJB 3.0 Packaging - Entities'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-114073113206187349</id><published>2006-02-23T21:27:00.000Z</published><updated>2006-11-04T22:44:43.494Z</updated><title type='text'>More EJB 3.0</title><content type='html'>For the past few days I have been working on a number of things. I shall blog about them in more detail when I have some time, but in the meantime, here is a summary:&lt;br /&gt;&lt;br /&gt;1. Packaging issues. EJB 3.0 brings hassle-free packaging, however, it is still not easy to understand what the rules are. A discussion on packaging issues can be found &lt;a href="http://forums.java.net/jive/thread.jspa?threadID=13253&amp;tstart=0"&gt;here&lt;/a&gt; and &lt;a href="http://forums.java.net/jive/thread.jspa?threadID=13092&amp;amp;tstart=0"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;2. Semantics of merge() and persist() and how they are implemented in Glassfish is being discussed &lt;a href="http://forums.java.net/jive/thread.jspa?threadID=13292&amp;tstart=0"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;3. Does EJB live up to its promise to bring POJO style programming to J2EE? I previously &lt;a href="http://trycatchfinally.blogspot.com/2006/01/ejb-30-entities-are-not-pojos.html"&gt;blogged&lt;/a&gt; about the fact that EJB 3.0 Entities are not really POJOs because they contain hidden semantics. See here for a discussion of &lt;a href="http://forums.java.net/jive/thread.jspa?threadID=13366&amp;amp;tstart=0"&gt;dependency injection&lt;/a&gt; of EJBs and whether it is possible to implement Business Logic as POJOs.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-114073113206187349?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/114073113206187349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=114073113206187349' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114073113206187349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/114073113206187349'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/02/more-ejb-30.html' title='More EJB 3.0'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113996728648543526</id><published>2006-02-15T01:14:00.000Z</published><updated>2006-11-04T22:44:43.418Z</updated><title type='text'>Sharing attributes across Entities</title><content type='html'>One way to share common attributes across entities is to use inheritance, and put the common attributes in a super class. Another way is to abstract the common attributes into a separate Embeddable class and then embed it within the regular Entity classes. The latter approach has been used for some of the entities in the &lt;a href="https://ejb3demo.dev.java.net/"&gt;EJB3Demo project&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The entities Customer, District and Warehouse all contain some very similar columns for storing address information. I created a new class called Address to hold this common data. The Address class is marked as @Embeddable and contains the getters/setters for the attributes. Here is an extract:&lt;br /&gt;&lt;pre&gt;@Embeddable public class Address implements Serializable {&lt;br /&gt;    String street1;&lt;br /&gt;    String city;&lt;br /&gt;    @Column(length=20)&lt;br /&gt;    public String getCity() {&lt;br /&gt;        return city;&lt;br /&gt;    }&lt;br /&gt;    @Column(length=20)&lt;br /&gt;    public String getStreet1() {&lt;br /&gt;        return street1;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;Note that the getters have been annotated using @Column - this is useful for capturing the bits that are shared across the entities.&lt;br /&gt;&lt;br /&gt;When you embed the Address object within an Entity, you can fine-tune the column mapping. Example:&lt;br /&gt;&lt;pre&gt;@Entity&lt;br /&gt;public class Customer {&lt;br /&gt;    Address address;&lt;br /&gt;    @Embedded&lt;br /&gt;    @AttributeOverrides({&lt;br /&gt;        @AttributeOverride(name="street1", column=@Column(name="C_STREET_1")),&lt;br /&gt;        @AttributeOverride(name="city", column=@Column(name="C_CITY")),&lt;br /&gt;    })&lt;br /&gt;    public Address getAddress() {&lt;br /&gt;        return address;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;The getter for the Address object is decorated with the @Embedded annotation. The address object can be shared amongst multiple entities, and each entity can define its own column names, etc.&lt;br /&gt;&lt;br /&gt;When you access embedded objects in EJBQL, you use the property name in the Query statement. For example, to access the city field in Customer, you would write:&lt;br /&gt;&lt;pre&gt;SELECT c.address.city FROM Customer c&lt;/pre&gt;This syntax is also used when you have relationship mappings. For example, a District belongs to a Warehouse, and hence the District object contains an instance of the Warehouse object. To access the fields within Warehouse, you would write:&lt;br /&gt;&lt;pre&gt;SELECT d.warehouse.name FROM District d&lt;/pre&gt;Although one could use inheritance to model above, I prefer to use the Embeddable class option as it more accurately reflects what is happening here. A Customer is not an instance of Address - a Customer has an Address. In this situation, Has-A relationship is more natural than Is-A relationship.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113996728648543526?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113996728648543526/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113996728648543526' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113996728648543526'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113996728648543526'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/02/sharing-attributes-across-entities.html' title='Sharing attributes across Entities'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113996585234125130</id><published>2006-02-15T01:02:00.000Z</published><updated>2006-11-04T22:44:43.346Z</updated><title type='text'>EJB3Demo project</title><content type='html'>I have started a &lt;a href="https://ejb3demo.dev.java.net"&gt;new project&lt;/a&gt; at &lt;a href="http://www.java.net"&gt;www.java.net&lt;/a&gt;. The goal of this project is to try out various features of Java EE 50, such as EJB 3.0, JAX-WS, JAXB, etc., and identify design/code patterns that result in:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Improved portability and reusability of code, in particular, allow Business Logic and Data Access logic to be deployed inside and outside J2EE containers.&lt;/li&gt;&lt;li&gt;Better separation of concerns while exploiting new persistence features of EJB 3.0.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;I hope to blog about some of this stuff soon - stay tuned. In the meantime, I would welcome anyone who would like to contribute to the project.&lt;/p&gt;&lt;p&gt;The source code for the project is available for download from &lt;a href="https://ejb3demo.dev.java.net/servlets/ProjectDocumentList"&gt;https://ejb3demo.dev.java.net/servlets/ProjectDocumentList&lt;/a&gt;. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113996585234125130?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113996585234125130/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113996585234125130' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113996585234125130'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113996585234125130'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/02/ejb3demo-project.html' title='EJB3Demo project'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113926229261935021</id><published>2006-02-06T21:14:00.000Z</published><updated>2006-11-04T22:44:43.284Z</updated><title type='text'>Reusability of Code</title><content type='html'>I have given up trying to get the TPCC schema work as defined, i.e., using composite keys. Glassfish at present does not support Id Generators with composite keys, which poses a problem, as it means writing extra code for generating keys. As a workaround to this issue, I have modified the schema to use surrogate primary keys.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;EJB persistence design favours Entity Relationships based upon surrogate primary keys.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;One of the great benefits of EJB 3.0 is that it gets rid of all the boiler plate EJB code, allowing you to code Entity and Session beans as if they were POJOs. This raises the expectation that in this new world, it should be possible to write reusable code that is environment agnostic. By this I mean that I should be able to reuse my J2EE Business Logic classes and the Persistent classes in a J2SE or Servlet Container (Tomcat) environment without having to change any code.&lt;br /&gt;&lt;br /&gt;There is an immediate stumbling block to achieving this, however. The unfortunate decision to introduce a new abstraction for Transaction Management for J2SE environment means that tranaction management code is not portable. The first rule for writing reusable code therefore is to avoid transaction management code in your classes. I will report on other pitfalls as I progress with my attempts to create reusable code.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113926229261935021?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113926229261935021/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113926229261935021' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113926229261935021'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113926229261935021'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/02/reusability-of-code.html' title='Reusability of Code'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113827319876478757</id><published>2006-01-26T10:52:00.000Z</published><updated>2006-11-04T22:44:43.212Z</updated><title type='text'>@GeneratedValue limitations</title><content type='html'>When I saw that the specification of sequence generation has been separated from the @Id annotation, I thought that this meant that sequence generators can be used for any column, and not just for surrogate primary keys. This, however, does not appear to be the case, at least in &lt;a href="https://glassfish.dev.java.net"&gt;Glassfish&lt;/a&gt;, where if you try to use a sequence generator on any column that is not defined as a surrogate primary key, you get an Exception. You cannot use sequence generators in composite primary keys either.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Update:&lt;/strong&gt; I could be wrong here because I am getting inconsistent results. More details are in the &lt;a href="https://glassfish.dev.java.net/issues/show_bug.cgi?id=192"&gt;bug report&lt;/a&gt; I have filed with the Glassfish team.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113827319876478757?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113827319876478757/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113827319876478757' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113827319876478757'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113827319876478757'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/generatedvalue-limitations.html' title='@GeneratedValue limitations'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113803240290537904</id><published>2006-01-23T15:44:00.000Z</published><updated>2006-11-04T22:44:43.150Z</updated><title type='text'>Mapping Relationships in EJB 3.0</title><content type='html'>Rather than create my own schema for testing EJB 3.0 features, I decided to use the schema used in &lt;a href="http://www.tpc.org/tpcc/spec/tpcc_current.pdf"&gt;TPC-C Benchmarks&lt;/a&gt;. This approach has the benefit that it forces me to design an object-relational mapping for a pre-existing schema, which is probably what most developers will do in practice.&lt;br /&gt;&lt;br /&gt;If you are designing a new schema, then bear in mind that EJB 3.0 favours a design where each table has a surrogate primary key, i.e., a meaningless numeric key that is either auto-generated or generated using a sequence. Using surrogate primary keys makes it easier to map the primary key of the table, as you can simply mark the relevant field using the @Id attribute. It also makes it easier to map relationships, as the EntityManager is able to exploit natural Has-A relationships (Composite design pattern) between objects. For example, in the TPC-C schema, an Order has a one-to-many relationship with OrderLines. This can be mapped as follows:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;An Order object should contain a Collection of OrderLine objects.&lt;/li&gt;&lt;li&gt;An OrderLine object should have a reference to the Order that it is related to.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Given below is the relevant code:&lt;br /&gt;&lt;pre&gt;public class Order {&lt;br /&gt;  set&amp;lt;OrderLine&amp;gt; orderLines;&lt;br /&gt;&lt;br /&gt;  @OneToMany(mappedBy="order")&lt;br /&gt;  public Set&amp;lt;OrderLine&amp;gt; getOrderLines() {&lt;br /&gt;    return orderLines;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class OrderLine {&lt;br /&gt;  Order order;&lt;br /&gt;&lt;br /&gt;  @ManyToOne&lt;br /&gt;  @JoinColumn(name="OL_O_ID", referencedColumnname="O_ID")&lt;br /&gt;  public Order getOrder() {&lt;br /&gt;    return order;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;An important point you must note here is that the OrderLine Entity does not contain an explicit field for the foreign key column "OL_O_ID" that references the primary key column "O_ID" in Order; the Persistence Engine automatically uses the appropriate field from the Order entity when inserting rows in the OrderLine table.&lt;br /&gt;&lt;br /&gt;The situation becomes more complex when tables are not designed to use surrogate primary keys. In this scenario, it is often necessary to use composite primary keys.&lt;br /&gt;&lt;br /&gt;I will use the Customer and District entities as examples because both have composite primary keys and there exists a many-to-one relationship between a Customer and a District. First of all, let's recall that to map a composite primary key, we need to create a primary key class that identifies the fields that form part of the composite key. This primary class is then associated with the Entity using the @IdClass annotation. The Entity itself also contains the fields used in the primary key; these are annotated using @Id and @Column. Since I described how this works in my &lt;a href="http://trycatchfinally.blogspot.com/2006/01/using-idclass-in-glassfish.html"&gt;previous post&lt;/a&gt;, I will only show the resulting key fields in the Customer entity here:&lt;br /&gt;&lt;pre&gt;@Entity&lt;br /&gt;public class Customer {&lt;br /&gt;&lt;br /&gt;  @Id&lt;br /&gt;  @Column(name="C_ID", nullable=false)&lt;br /&gt;  public int getCustomerId() {&lt;br /&gt;    return customerId;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @Id&lt;br /&gt;  @Column(name="C_D_ID", nullable=false)&lt;br /&gt;  public int getDistrictId() {&lt;br /&gt;    return districtId;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @Id&lt;br /&gt;  @Column(name="C_W_ID", nullable=false)&lt;br /&gt;  public int getWarehouseId() {&lt;br /&gt;    return warehouseId;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Now in order to map the relationships, we add the following:&lt;br /&gt;&lt;pre&gt;@Entity&lt;br /&gt;public class Customer {&lt;br /&gt;  District district;&lt;br /&gt;&lt;br /&gt;  @ManyToOne&lt;br /&gt;  @JoinColumns({&lt;br /&gt;    @JoinColumn(name="C_W_ID", referencedColumnName="D_W_ID"),&lt;br /&gt;    @JoinColumn(name="C_D_ID", referencedColumnName="D_ID")&lt;br /&gt;  })&lt;br /&gt;  public District getDistrict() {&lt;br /&gt;    return district;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Entity&lt;br /&gt;public class District {&lt;br /&gt;&lt;br /&gt;  Set&amp;lt;customer&amp;gt; customers;&lt;br /&gt;&lt;br /&gt;  @OneToMany(mappedBy="district")&lt;br /&gt;  public Set&amp;lt;customer&amp;gt; getCustomers() {&lt;br /&gt;    return customers;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The problem is that now there are two ways of updating the foreign key columns in the Customer entity, because the foreign key columns are present in the referenced District entity, but also present as fields in the entity in order to satisfy the requirements for a composite primary key. If you try to execute this code, you will encounter an Exception such as this (in Glassfish):&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Multiple writable mappings exist for the field [TPCC.CUSTOMER.C_W_ID]. Only one may be defined as writable, all others must be specified read-only.&lt;br /&gt;&lt;br /&gt;Multiple writable mappings exist for the field [TPCC.CUSTOMER.C_D_ID]. Only one may be defined as writable, all others must be specified read-only.&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;To resolve this problem, you need to modify the Customer Entity definition and ensure that the columns TPCC.CUSTOMER.C_W_ID and TPCC.CUSTOMER.C_D_ID are marked as readonly, by setting insertable=false and updatable=false:&lt;br /&gt;&lt;pre&gt;@Entity&lt;br /&gt;public class Customer {&lt;br /&gt;&lt;br /&gt;  @Id&lt;br /&gt;  @Column(name="C_D_ID", nullable=false,&lt;br /&gt;          insertable=false, updatable=false)&lt;br /&gt;  public int getDistrictId() {&lt;br /&gt;    return districtId;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  @Id&lt;br /&gt;  @Column(name="C_W_ID", nullable=false,&lt;br /&gt;          insertable=false, updatable=false)&lt;br /&gt;  public int getWarehouseId() {&lt;br /&gt;    return warehouseId;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113803240290537904?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113803240290537904/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113803240290537904' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113803240290537904'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113803240290537904'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/mapping-relationships-in-ejb-30.html' title='Mapping Relationships in EJB 3.0'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113771480408901332</id><published>2006-01-19T23:17:00.000Z</published><updated>2006-11-04T22:44:43.089Z</updated><title type='text'>EJB 3.0 Entities are not POJOs</title><content type='html'>Contrary to appearances, EJB 3.0 Entities are not POJOs as they contain hidden data and functionality in order to implement some of the specification requirements. I found this out when I was stepping through some code and out of curiosity, inspected the entities returned by the EntityManager.&lt;br /&gt;&lt;br /&gt;When you implement two Entities that are related, for example, in a one-to-many relationship, you typically have a Collection class in the first entity that contains references to instances of the second entity. For example, an Order entity may contain a Collection of OrderLine entities. EJB 3.0 supports "lazy fetches", which means that the data for the Collection is not fetched until required. To support this, the Collection class contains extra functionality and data - you can see this if you inspect the Entity using a debugger. When you try to access the collection, this hidden functionality is triggered; and data is fetched to populate the Collection. This process carries on recursively for all referenced entities.&lt;br /&gt;&lt;br /&gt;Lazy fetching is clearly desirable because you do not want data to be unnecessarily fetched if you do not intend to use it. However, the problem is that the Entity is not fully initialized until all the data is fetched. This poses a particular problem if you want to use the Entities outside of their "managed" environment. Any attempt to access the uninitialized data may fail. Section 3.2.4 of the EJB 3.0 specification (PFD) spells out exactly what you can safely access.&lt;br /&gt;&lt;br /&gt;See the &lt;a href="http://forums.java.net/jive/thread.jspa?forumID=56&amp;threadID=2595"&gt;recent thread&lt;/a&gt; at the Glassfish Discussion Forums on this issue, and how it affects client access.&lt;br /&gt;&lt;br /&gt;One of the questions that will be debated in future is whether EJB Entities should be exposed outside the Data Access layer. For example, should you expose entities in your Business Logic interfaces, or to your clients. Since EJB 3.0 Entities are touted as POJOs, many developers will assume that they can use these objects ouside of the Data Access layer. However, such use is fraught with danger, due to the semantics of detatched entities.&lt;br /&gt;&lt;br /&gt;In my view, the EJB specification should require that detached entities are "cleaned" and made POJOs. This would lead to more predictable behaviour, and less surprises.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113771480408901332?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113771480408901332/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113771480408901332' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113771480408901332'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113771480408901332'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/ejb-30-entities-are-not-pojos.html' title='EJB 3.0 Entities are not POJOs'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113750555214606410</id><published>2006-01-17T13:27:00.000Z</published><updated>2006-11-04T22:44:43.029Z</updated><title type='text'>Some comments on EJB 3.0 PFD</title><content type='html'>In a previous &lt;a href="http://trycatchfinally.blogspot.com/2006/01/problems-with-ejb-30-idclass.html"&gt;post&lt;/a&gt;, I complained about lack of clarity in the specification of the @IdClass annotation. Well, it seems that this lack of clarity extends to anything to do with composite primary keys. Here is what the specification has to say about composite primary keys:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;Composite primary keys typically arise when mapping from legacy databases when the database key is comprised of several columns.&lt;/blockquote&gt;Perhaps this explains why there aren't any real examples to show how Entity relationships ought to be mapped when composite primary keys are involved.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113750555214606410?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113750555214606410/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113750555214606410' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113750555214606410'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113750555214606410'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/some-comments-on-ejb-30-pfd.html' title='Some comments on EJB 3.0 PFD'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113741941584801382</id><published>2006-01-16T13:31:00.000Z</published><updated>2006-11-04T22:44:42.967Z</updated><title type='text'>@GeneratedValue annotation support in Glassfish</title><content type='html'>I reported &lt;a href="http://trycatchfinally.blogspot.com/2006/01/experiments-with-ejb-30-id-annotation.html"&gt;previously&lt;/a&gt; that Glassfish does not yet support the new @GeneratedValue annotation specified in the &lt;a href="http://jcp.org/aboutJava/communityprocess/pfd/jsr220/index.html"&gt;EJB 3.0 PFD&lt;/a&gt;. I downloaded the latest build ( &lt;a href="https://glassfish.dev.java.net/public/downloadsindex.html"&gt;b33&lt;/a&gt;) of Glassfish today, and while testing my code, found that the new build supports @GeneratedValue. It is a pity that one has to find this out by trail and error; the Glassfish team should put up some release notes with each build which covers feature changes since last build.&lt;br /&gt;&lt;br /&gt;Anyway, the old syntax for @Id annotation allowed atributes for specifying Id Generators. The new method requires the Id Generator to be specified separately using a @GeneratedValue annotation. Here is a comparison between the old approach and the new approach:&lt;br /&gt;&lt;br /&gt;Old approach:&lt;br /&gt;&lt;pre&gt;@Id (generate=GeneratorType.SEQUENCE, generator="SEQ_GEN") &lt;br /&gt;public Long id;&lt;/pre&gt;New approach:&lt;br /&gt;&lt;pre&gt;@Id&lt;br /&gt;@GeneratedValue(strategy=GenerationType.SEQUENCE, &lt;br /&gt;  generator="SEQ_GEN")&lt;br /&gt;public Long id;&lt;/pre&gt;I think the new approach is cleaner.  It also enables Id Generator to be used in fields that are not necessarily the primary key.&lt;br /&gt;&lt;br /&gt;Overall, the fact that Glassfish seems to be following the draft specification closely is great, as it enables developers to get familiar with the new technology. Thankfully, the specs are now close to final version, so changes ought to be relatively small.&lt;br /&gt;&lt;br /&gt;Talking of changes, I hit another one. The syntax for @TableGenerator has changed. The table is not longer specified using @Table, instead, table and schema are ordinary string attributes. Here's an example of the new syntax:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;@TableGenerator(name = "DISTRICT_ID_SEQUENCE", &lt;br /&gt;   table = "IDGENERATOR", schema = "TPCC", &lt;br /&gt;   pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", &lt;br /&gt;   pkColumnValue = "DISTRICT_ID_SEQUENCE", &lt;br /&gt;   allocationSize = 10)&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113741941584801382?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113741941584801382/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113741941584801382' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113741941584801382'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113741941584801382'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/generatedvalue-annotation-support-in.html' title='@GeneratedValue annotation support in Glassfish'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113706481714648590</id><published>2006-01-12T11:08:00.000Z</published><updated>2006-11-04T22:44:42.899Z</updated><title type='text'>Regarding EJB 3.0 Entity lifecycle</title><content type='html'>EJB 3.0 Entities exist in one of 4 states: new, managed, detached, and removed.&lt;br /&gt;&lt;br /&gt;The state transitions of an Entity depend upon the type of operations on the Entity and the life-cycles of associated Persistence and Transaction Contexts.&lt;br /&gt;&lt;br /&gt;When an entity is first instantiated using Java new operator, its state is "new".&lt;br /&gt;&lt;br /&gt;If you obtain an entity instance via EntityManager.find() or EntityManager.getReference(), or through a Query, its state depends upon whether there is an active Persistence Context or not. If there is one, then the Entity is in "managed" state, otherwise in "detached" state.&lt;br /&gt;&lt;br /&gt;When you invoke EntityManager.persist() on a "new", "managed" or "removed" Entity, it becomes "managed". Note that you cannot invoke persist() on a "detached" Entity.&lt;br /&gt;&lt;br /&gt;When you invoke EntityManager.merge() on a "new" or "detached" Entity, a "managed" copy of the Enity is created. You cannot invoke merge() on a "removed" Entity. Invoking merge() on a "managed" Entity leaves it in "managed" state.&lt;br /&gt;&lt;br /&gt;When you invoke EntityManager.remove() on a "managed" Entity, its new state becomes "removed". Attempts to remove "new" or "removed" entities are ignored, however, it is an error to try and remove a "detached" entity.&lt;br /&gt;&lt;br /&gt;If the Persistence Context is TRANSACTION scoped, then all "managed" entities within the Persistence Context become "detached" when the associated transaction ends. If the Persistence Context is scoped as EXTENDED, then the entities remain "managed" even after transaction ends.&lt;br /&gt;&lt;br /&gt;In terms of Entity state, "new" and "detached" states are similar in that the Persistence Context has no knowledge of such entities.&lt;br /&gt;&lt;br /&gt;The rules governing the lifecycle of Persistence Contexts are complex; I will explore them in another post.&lt;br /&gt;&lt;br /&gt;The EntityManager interface provides two mechanisms for persisting an Entity: persist() and merge(). The differences between these two interfaces are subtle, and in my view, a single interface would have been better and less confusing. In terms of usage, I would recommend using merge() at all times.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113706481714648590?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113706481714648590/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113706481714648590' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113706481714648590'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113706481714648590'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/regarding-ejb-30-entity-lifecycle.html' title='Regarding EJB 3.0 Entity lifecycle'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113689800452253318</id><published>2006-01-10T12:57:00.000Z</published><updated>2006-11-04T22:44:42.840Z</updated><title type='text'>Using @IdClass in Glassfish</title><content type='html'>Given below is an example of how to use @IdClass in Glassfish. Note that the primary key class must not have any members other than the Id fields, not even serialVersionUID, even though the primary key class is defined as Serializable.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;@Entity&lt;br /&gt;@Table(name = "WAREHOUSE", schema = "TPCC")&lt;br /&gt;@IdClass(entity.tpcc.WarehousePK.class)&lt;br /&gt;public class Warehouse {&lt;br /&gt;&lt;br /&gt;    String name;&lt;br /&gt;    String city;&lt;br /&gt;&lt;br /&gt;    @Id&lt;br /&gt;    @Column(name = "W_NAME", length = 10)&lt;br /&gt;    public String getName() {&lt;br /&gt;        return name;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    @Id&lt;br /&gt;    @Column(name = "W_CITY", length = 20)&lt;br /&gt;    public String getCity() {&lt;br /&gt;        return city;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void setCity(String city) {&lt;br /&gt;        this.city = city;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void setName(String name) {&lt;br /&gt;        this.name = name;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class WarehousePK implements Serializable {&lt;br /&gt;&lt;br /&gt;    String name = "";&lt;br /&gt;    String city = "";&lt;br /&gt;&lt;br /&gt;    public WarehousePK() {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public String getCity() {&lt;br /&gt;        return city;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public String getName() {&lt;br /&gt;        return name;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void setCity(String city) {&lt;br /&gt;        this.city = city;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void setName(String name) {&lt;br /&gt;        this.name = name;&lt;br /&gt;    }&lt;br /&gt;  &lt;br /&gt;    @Override&lt;br /&gt;    public boolean equals(Object arg0) {&lt;br /&gt;        if (arg0 == this) {&lt;br /&gt;            return true;&lt;br /&gt;        }&lt;br /&gt;        else if (arg0 instanceof WarehousePK) {&lt;br /&gt;            return name.equals(((WarehousePK)arg0).name) &amp;&amp;amp;&lt;br /&gt;  city.equals(((WarehousePK)arg0).city);&lt;br /&gt;        }&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    @Override&lt;br /&gt;    public int hashCode() {&lt;br /&gt;        return name.hashCode() ^ city.hashCode();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;The primary key class is useful when searching for objects by their primary key. Example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;    WarehousePK key = new WarehousePK();&lt;br /&gt;    key.setName("LONDON WAREHOUSE");&lt;br /&gt;    key.setCity("LONDON");&lt;br /&gt;    Warehouse w = em.find(Warehouse.class, key);&lt;/pre&gt;When creating new entities of updating existing entities, you should access the properties within the entity as normal. Example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;    Warehouse w = new Warehouse();&lt;br /&gt;    w.setName("LONDON WAREHOUSE");&lt;br /&gt;    w.setCity("LONDON");&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113689800452253318?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113689800452253318/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113689800452253318' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113689800452253318'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113689800452253318'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/using-idclass-in-glassfish.html' title='Using @IdClass in Glassfish'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113689483936037994</id><published>2006-01-10T12:05:00.000Z</published><updated>2006-11-04T22:44:42.782Z</updated><title type='text'>Problems with EJB 3.0 @IdClass specification</title><content type='html'>Note that comments below are based upon the Proposed Final Draft specification of EJB 3.0.&lt;br /&gt;&lt;br /&gt;I feel that the specification of @IdClass is not at all clear. There are several problems:&lt;br /&gt;&lt;br /&gt;1. It is not clear what the class that represents the composite primary key is used for. &lt;br /&gt;&lt;br /&gt;Is it just an informative construct designed to tell the Enity Manager which fields of the Entity represent the key? &lt;br /&gt;&lt;br /&gt;How and when are clients expected to use this class? &lt;br /&gt;&lt;br /&gt;From trial and error and guesswork I have deduced that the class that represents primary key is essentially for two reasons:&lt;br /&gt;&lt;br /&gt;a) First to tell the Entity Manager which fields of an Entity correspond to the composite primary key.&lt;br /&gt;b) Secondly, for use in the EntityManager.find() and EntityManager.getReference() methods, as the second argument.&lt;br /&gt;&lt;br /&gt;It is also not clearly stated anywhere whether the primary key class needs to be annotated in any manner. Clearly, if it is used as @EmbeddedId then it must be annotated using @Embeddable, but if it is used as @IdClass then no annotation is needed.&lt;br /&gt;&lt;br /&gt;2. There aren't any examples illustrating the use of @IdClass. There are few code snippets that depict @IdClass usage, but these do not adhere to the requirements defined for primary key classes.&lt;br /&gt;&lt;br /&gt;Section 9.1.13 shows an entity called Employee, but does not show the EmployeePK class.&lt;br /&gt;&lt;br /&gt;Section 9.1.31 (page 190) shows a primary key class EmpPK but this class does not adhere to the rules - ie, not Serializable, and does not implement hashCode() and equals().&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113689483936037994?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113689483936037994/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113689483936037994' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113689483936037994'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113689483936037994'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/problems-with-ejb-30-idclass.html' title='Problems with EJB 3.0 @IdClass specification'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113680699514992611</id><published>2006-01-09T11:28:00.000Z</published><updated>2006-11-04T22:44:42.726Z</updated><title type='text'>Using bind variables in generated SQLs</title><content type='html'>I noticed that the generated SQLs in &lt;a href="https://glassfish.dev.java.net"&gt;Glassfish/Toplink&lt;/a&gt; implementation of EJB 3.0 persistence, use literal values instead of bind variables. Here are a few examples:&lt;br /&gt;&lt;br /&gt;Here is the generated SQL when creating a new entity:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;INSERT INTO TPCC.WAREHOUSE&lt;br /&gt;   (W_CITY, W_STREET_1, W_STREET_2, W_VERSION, &lt;br /&gt;      W_TAX, W_STATE, W_YTD, W_NAME, W_ZIP)&lt;br /&gt;   VALUES ('LONDON', NULL, NULL, 1, 0, NULL, 0, &lt;br /&gt;      'LONDON WAREHOUSE', NULL) &lt;/pre&gt;Now, look at the SQL generated for a query:&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;Warehouse w = (Warehouse) em.createQuery(&lt;br /&gt;   "SELECT w FROM Warehouse w WHERE w.name = :wname")&lt;br /&gt;     .setParameter("wname", "LONDON WAREHOUSE")&lt;br /&gt;     .getSingleResult();&lt;/pre&gt;Generated SQL:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;SELECT W_ID, W_CITY, W_STREET_1, W_STREET_2, &lt;br /&gt;      W_VERSION, W_TAX, W_STATE, W_YTD, W_NAME, W_ZIP &lt;br /&gt;   FROM TPCC.WAREHOUSE &lt;br /&gt;   WHERE (W_NAME = 'LONDON WAREHOUSE')&lt;/pre&gt;Finally, have a look at the UPDATE SQL:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;UPDATE TPCC.WAREHOUSE &lt;br /&gt;   SET W_STREET_1 = 'Braham Street', W_VERSION = 2 &lt;br /&gt;   WHERE ((W_ID = 1) AND (W_VERSION = 1))&lt;/pre&gt;Clearly, the default implementation is always using literals in SQL statements. I haven't yet found a way to tell Glassfish/Toplink to use bind variables in SQLs.&lt;br /&gt;&lt;br /&gt;Fortunately, since Glassfish and Toplink are both OpenSource, I can probably find out how to do this by looking at the source code.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113680699514992611?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113680699514992611/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113680699514992611' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113680699514992611'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113680699514992611'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/using-bind-variables-in-generated-sqls.html' title='Using bind variables in generated SQLs'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113680607087634974</id><published>2006-01-09T11:06:00.000Z</published><updated>2006-11-04T22:44:42.668Z</updated><title type='text'>Why EntityTransaction?</title><content type='html'>When managing entities outside a J2EE container, you need to obtain an instance of EntityTransaction to manage transactions. Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;        EntityManager em = null;&lt;br /&gt;        try {&lt;br /&gt;            em = Persistence.createEntityManagerFactory("em1")&lt;br /&gt;                    .createEntityManager();&lt;br /&gt;            System.err.println("Created entity manager");&lt;br /&gt;            Warehouse w = new Warehouse();&lt;br /&gt;            w.setName("LONDON WAREHOUSE");&lt;br /&gt;            w.setCity("LONDON");&lt;br /&gt;           &lt;br /&gt;            EntityTransaction t = em.getTransaction();&lt;br /&gt;           &lt;br /&gt;            boolean success = false;&lt;br /&gt;            t.begin();&lt;br /&gt;            try {&lt;br /&gt;                em.persist(w);&lt;br /&gt;                success = true;&lt;br /&gt;            }&lt;br /&gt;            finally {&lt;br /&gt;                if (success) {&lt;br /&gt;                    t.commit();&lt;br /&gt;                }&lt;br /&gt;                else {&lt;br /&gt;                    t.rollback();&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;           &lt;br /&gt;        } catch (Exception e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        }&lt;br /&gt;        finally {&lt;br /&gt;            if (em != null) {&lt;br /&gt;                em.close();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;I would like to understand the rationale for introducing a new abstraction for transaction management when the UserTransaction interface already exists. By introducing a different mechanism for out-of-container applications, it will be harder to write code that is environment agnostic ... unless you write a wrapper to hide the differences.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113680607087634974?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113680607087634974/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113680607087634974' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113680607087634974'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113680607087634974'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/why-entitytransaction.html' title='Why EntityTransaction?'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113676451260256536</id><published>2006-01-08T23:18:00.000Z</published><updated>2006-11-04T22:44:42.607Z</updated><title type='text'>Experiments with EJB 3.0 @Id annotation</title><content type='html'>I have been messing around with @Id annotation in EJB 3.0 for generating primary keys.&lt;br /&gt;&lt;br /&gt;Firstly, there appears to be a change in the latest version (&lt;a href="http://jcp.org/aboutJava/communityprocess/pfd/jsr220/index.html"&gt;Proposed Final Draft&lt;/a&gt;) of the EJB 3.0 specification which is not supported in Glassfish yet. The latest version requires a separate annotation called @GeneratedValue to define the type of Id generator. I downloaded Eclipse &lt;a href="http://www.eclipse.org/dali/"&gt;Dali&lt;/a&gt; yesterday, and this seems to support the new method.&lt;br /&gt;&lt;br /&gt;The previous syntax supported five generator types - NONE, AUTO, SEQUENCE, IDENTITY and TABLE. In the new syntax, NONE is no longer required, as it is implied if there is no @GeneratedValue annotation. The remaining four generator types are supported.&lt;br /&gt;&lt;br /&gt;By default, if no generator type is specified, then NONE is implied. This means that the developer must set the Id property or field correctly before attempting to persist the entity.&lt;br /&gt;&lt;br /&gt;I have some trouble getting to terms with SEQUENCE and IDENTITY generators. Not all databases support both of these, so it seems to me that if you annotate your Entity with either of these then you are going to end up with non-portable code. My recommendation is to avoid these.&lt;br /&gt;&lt;br /&gt;IDENTITY generator type is meant to be used when the underlying DBMS supports the IDENTITY column type - this is an autoincrement column that is maintained by the DBMS itself. For example, you can create a table in &lt;a href="http://db.apache.org/derby/"&gt;Apache Derby&lt;/a&gt; with a column defined as "GENERATED ALWAYS AS IDENTITY". Note that systems that support this also support a mechanism for retrieving the last generated IDENTITY value. This method is not the equivalent of using a trigger to populate the primary key - as is sometimes done in Oracle. With the trigger approach, there is no support for retrieving the last generated IDENTITY value.&lt;br /&gt;&lt;br /&gt;The SEQUENCE generator is meant for systems that support Sequences. I know of Oracle and PostgreSQL that support sequences.&lt;br /&gt;&lt;br /&gt;The TABLE generator, as its name implies, relies upon a special table for generating sequences. This is a portable method, as it does not rely upon native generators. Here is an example annotation that specifies a table generator:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;@TableGenerator(name="IDGEN",&lt;br /&gt;   table=@Table(name = "SEQUENCE", schema = "DIBYENDU"),&lt;br /&gt;   pkColumnName="SEQ_NAME",&lt;br /&gt;   valueColumnName="SEQ_COUNT",&lt;br /&gt;   pkColumnValue="ID_SEQUENCE",&lt;br /&gt;   allocationSize=10)&lt;/pre&gt;Finally, there is the AUTO generator type. This is meant to use the best strategy available in the DBMS. Unfortunately, Glassfish seems to insist on using a table generator, at least when I tested this with Apache Derby. It also assumes that you have created a table called SEQUENCE in the default schema for the JDBC Connection ID - there is no way to specify the schema. The expected table structure is not documented as far as I can see, but you can guess it by looking at the SQL logs. You can also use the DDL generation facility in Glassfish to get the basic DDL for creating this table.&lt;br /&gt;&lt;br /&gt;Speaking of DDL generation facility in Glassfish, I think it is not very useful at the moment. The DDL generated looses information - for example, it does not respect any @Column attributes other than name.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113676451260256536?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113676451260256536/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113676451260256536' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113676451260256536'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113676451260256536'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/experiments-with-ejb-30-id-annotation.html' title='Experiments with EJB 3.0 @Id annotation'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113665804845668124</id><published>2006-01-07T18:05:00.000Z</published><updated>2006-11-04T22:44:42.549Z</updated><title type='text'>On Checked Exceptions</title><content type='html'>Here are a couple of my blogs on Checked Exceptions:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;a href="http://simpledbm.blogspot.com/2005/09/in-favour-of-checked-exceptions.html"&gt;Why I favour Checked Exceptions&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;&lt;a href="http://simpledbm.blogspot.com/2006/01/more-on-exceptions.html"&gt;Technique for ensuring method signature stability&lt;/a&gt;.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;I recommend this excellent &lt;a href="http://www.artima.com/weblogs/viewpost.jsp?thread=142428"&gt;blog&lt;/a&gt; about Java API Design Guidelines, by Eamonn McManus. Agree with most of it except the bit about using Unchecked Exceptions. Eamonn provides a link to a &lt;a href="http://lcsd05.cs.tamu.edu/slides/keynote.pdf"&gt;presentation&lt;/a&gt; by Joshua Bloch, which covers the same subject.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113665804845668124?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113665804845668124/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113665804845668124' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113665804845668124'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113665804845668124'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/on-checked-exceptions.html' title='On Checked Exceptions'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113646155699884817</id><published>2006-01-05T11:31:00.000Z</published><updated>2006-11-04T22:44:42.485Z</updated><title type='text'>Using Glassfish EJB 3.0 Persistence in J2SE</title><content type='html'>&lt;strong&gt;UPDATED 7th May 2006&lt;br /&gt;&lt;/strong&gt;One of the great things about EJB 3.0 is its support for persistence in J2SE environments. This is great for trying out various persistence features without the overhead of developing an application which must be deployed to an application server before it can be tested.&lt;br /&gt;&lt;br /&gt;To use EJB persistence in your J2SE program, you need a couple of jar files that come with Glassfish distribution. These are:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;/glassfish/lib/toplink-essentials.jar&lt;/li&gt;&lt;li&gt;/glassfish/lib/toplink-essentials-agent.jar&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;These jar files are available as a separate standlone bundle &lt;a href="https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html"&gt;here&lt;/a&gt;.&lt;/p&gt;You require the toplink-essentials.jar during development. At runtime, you need to add the following option to your Java command line:&lt;br /&gt;&lt;br /&gt;-javaagent:/glassfish/lib/toplink-essentials-agent.jar&lt;br /&gt;&lt;br /&gt;This will automatically include the toplink-essentials.jar to your classpath.&lt;br /&gt;&lt;br /&gt;There is another preliminary step that you need to be aware of. You need to create a file named persistence.xml in a directory called META-INF. This directory must be in your classpath. Given below is an example of a persistence.xml file:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"&amp;gt;&lt;br /&gt;  &amp;lt;persistence-unit name="em1" transaction-type="RESOURCE_LOCAL"&amp;gt;&lt;br /&gt;    &amp;lt;provider&amp;gt;oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider&amp;lt;/provider&amp;gt;&lt;br /&gt;    &amp;lt;class&amp;gt;entity.Table&amp;lt;/class&amp;gt;&lt;br /&gt;    &amp;lt;properties&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" /&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:sample" /&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.jdbc.user" value="scott" /&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.jdbc.password" value="tiger" /&amp;gt;&lt;br /&gt;        &amp;lt;property name="toplink.logging.level" value="INFO" /&amp;gt;&lt;br /&gt;    &amp;lt;/properties&amp;gt;&lt;br /&gt;  &amp;lt;/persistence-unit&amp;gt;&lt;br /&gt;&amp;lt;/persistence&amp;gt;&lt;br /&gt;&lt;/pre&gt;If you use set logging level to FINEST you can see the SQLs being generated by Toplink.&lt;br /&gt;&lt;br /&gt;To access the EJB 3.0 EntityManager in your program, add lines similar to following:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;  EntityManager em = Persistence.createEntityManagerFactory("em1")&lt;br /&gt;      .createEntityManager();&lt;br /&gt;&lt;/pre&gt;&lt;strong&gt;&lt;/strong&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113646155699884817?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113646155699884817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113646155699884817' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113646155699884817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113646155699884817'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/using-glassfish-ejb-30-persistence-in.html' title='Using Glassfish EJB 3.0 Persistence in J2SE'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113641680582392150</id><published>2006-01-04T23:13:00.000Z</published><updated>2006-11-04T22:44:42.425Z</updated><title type='text'>EJB 3.0</title><content type='html'>I have recently started exploring EJB 3.0. I am using the latest builds of &lt;a href="https://glassfish.dev.java.net/"&gt;Glassfish&lt;/a&gt;, Sun's OpenSource Application Server. Glassfish contains an OpenSource version of Oracle's Toplink product.&lt;br /&gt;&lt;br /&gt;I had stayed away from EJB so far, as I was not convinced that the benefits of EJB outweighed the complexities it introduced into code. I had been looking at alternative light weight frameworks such as &lt;a href="http://www.springframework.org"&gt;SpringFramework&lt;/a&gt; as a substitute for EJB. EJB 3.0 has however converted me. In my view, once production implementations of EJB 3.0 are available, there will be fewer occasions to use alternative frameworks. &lt;br /&gt;&lt;br /&gt;As I learn more about EJB 3.0 and Glassfish, I will post my findings here. Stay tuned.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113641680582392150?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113641680582392150/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113641680582392150' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113641680582392150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113641680582392150'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/ejb-30.html' title='EJB 3.0'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20547117.post-113641616437487185</id><published>2006-01-04T23:08:00.000Z</published><updated>2006-11-04T22:44:42.365Z</updated><title type='text'>Hello</title><content type='html'>This is where I intend to blog about programming in general, and Java programming in particular.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/20547117-113641616437487185?l=trycatchfinally.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://trycatchfinally.blogspot.com/feeds/113641616437487185/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20547117&amp;postID=113641616437487185' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113641616437487185'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20547117/posts/default/113641616437487185'/><link rel='alternate' type='text/html' href='http://trycatchfinally.blogspot.com/2006/01/hello.html' title='Hello'/><author><name>Dibyendu Majumdar</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
