Thursday, November 01, 2007

EJB3.0 Persistence Relationships

The EJB 3.0 persistence relationships via annotations always elude me, so I figured I should take note of them here now that I got it right (finally).
Suppose you want to express the idea that an activity is done on one task at a time by one user. Three objects are involved: Activity, User, Task. To build a database relation between these:
Activity(n)<->(1)User = @ManyToOne in Activity, @OneToMany in User. :: Activity can be associated with only one user while User can be associated with many activities.
Activity(n)<->(1)Task = @ManyToOne in Activity, @OneToMany in Task. :: Activity can be associated with only one task while Task can be associated with many activities.
User(1)<->(n)Task = @OneToMany in User, @ManyToOne in Task. :: A user can have many tasks, but a task can be owned by at most one user.
@ManyToMany is trivial and the commonest used.

For a person without the mental picture of database relationships, these simple rules will help.

Tuesday, October 30, 2007

Glassfish V2 Not Ready

Glassfish V2 comes bundled with NetBeans 6.0 Beta versions, and it will be disappointing to find out that it will fail to compile JSPs when you deploy a simple web application. The exception it throws is:

java.lang.NoSuchMethodError: javax.tools.ToolProvider.getSystemJavaCompiler()Ljavax/tools/JavaCompiler

This is solely as Glassfish issue. Calling "java" from the command line shows a Java compiler is available systemwide. Moving the application to an existing SJSAS9 is successful - no such issue is experienced. So, if you have NetBeans 6.0 Beta 2, you should use the Sun Java System Application Server 9.x (separate install) for your Java EE 5 applications. No need to try and troubleshoot. I think a ticket has been logged with Glassfish developers.

Saturday, September 29, 2007

Stale MySql Connections In SJSAS

If you use the Sun Java System Application Server (SJSAS) with the MySQL database application, you will eventually notice that communication between the two products "goes to sleep" after prolonged periods of inactivity. I've traced this to the following root cause:
: Communications link failure due to underlying exception:

java.io.IOException: An established connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:33)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:104)
at sun.nio.ch.IOUtil.write(IOUtil.java:75)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:334)
at com.sun.enterprise.server.ss.provider.ASOutputStream.write(ASOutputStream.java:138)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2637)

Something goes stale during down time and it can't be revived until you restart the server itself. Restarting the database doesn't help. For servers always being accessed, the issue won't be seen. I'd most likely blame the application server if it were up to me, but I'm not conclusive on that, since it may also mean that a socket connection to MySQL's port failed. Just not sure!
An easy workaround is to implement a thread in the application that keeps exercising the connection if it hasn't been used in a specified period of time. When I did this, my problem disappeared.

Friday, September 28, 2007

Weird Windows