Netbeans and JPA development Top Tip – Remember to clean build!
Posted by Paul Carey in Uncategorized on October 7th, 2009
A top tip for those of you doing JPA development within Netbeans or even without Netbeans, something I stupidly forgot to do was to clean build after editing the persistence.xml file!
This effectivly meant that my new Entity classes where not being picked up and the persistence provider (EclipseLink) was throwing its legs in the air!
So remember always recompile after changing the persistence.xml file to ensure your changes are noticed by the Java Persistence Api!
Dynamic GUI through Java Bean introspection
Posted by Paul Carey in java on March 16th, 2009
For yet another part of my dissertation project, it recently became evident to me that I would benefit from having the mobile part of my application to auto generate GUI forms. These forms would be based on the properties of Java beans. Yet again Java came to the rescue through its beans introspection API. The introspection API allows you to not only inspect the properties of a Java bean but also to discover and call the methods (read/write) that are associated to those properties.
Instead of boring you with the details I have uploaded a quick and commented example of using this API in order to be able to build dynamic GUI’s based on the fed Java Beans. This is only a basic example and currently only deals with String and Integer values though this can easily be adapted to deal with more and complex data types by changing the readProps() and writeProps() methods.
Example Dynamic GUI’s from Java Beans (Netbeans) Project Download
Or, individual sources
http://paulcarey.co.uk/releases/dynamicguiexample/DynamicGui.java
http://paulcarey.co.uk/releases/dynamicguiexample/Company.java
hack in pessimistic transactions with savepoint rollbacks into your app with EclipseLink and JPA
Posted by Paul Carey in java on February 22nd, 2009
Recently trying to implement pessimistic locks with the Java Persistence API and Eclipse-Link turned out to be not so straightforward…
Firstly the official JPA specification doesn’t support pessimistic locking, instead only optimistic. The diffrence being that pessimistic will lock clients out from records completly (at least with write locks) and optimistic locks rely on versionining to check if a record/object has changed since it was read before writing and then flags an error if it has.
In my current appliucation I’m writing for my dissertation I need pessimistic locks this is to ensure (obviously) that no two users can edit the same record at the same time.
The solution to this was to use EclipseLink, though ensuring that the underlying database also supported the ability to place locks on records. MySQL with the innodb engine does support this. However as EclipseLink originated from TopLink originally written by Oracle it is no surprise that it is oriented towards the Oracle DB rather then MySQL or any other DB.
The only problem this really causes is that when using the EcliseLink PessimisticLock options with MySQL ensure that you only use the “PessimisticLock.Lock” option and not “PessimisticLock.LockNoWait”. The problem is that MySQL does not support the SELECT …. FOR UPDATE NOWAIT feature as Oracale does but only standard SELECT …. FOR UPDATE.
To get around this additionally set your innodb_lock_wait_timeout=0 in my.cnf that wait time to attain locks is minimal. If locks are held on the required object then eventually (after a short period of time) a DatabaseException will be thrown this can in turn be caught to alert the user to the problem.
The next problem then comes in allowing rollbacks, again JPA does not support rollbacks and neither does EclipseLink in this case within the context of JPA or nativelly using DBMS feature (though EclipseLink run nativelly does have UnitOfWork).
To get around these problems see the following code snippet..
EntityManager em;
…
public Savepoint getSavepoint() throws SQLException {
em.flush();
//Get the SQL connection
Connection conn = ((UnitOfWorkImpl) ((JpaEntityManager) em).getActiveSession().acquireUnitOfWork()).getAccessor().getConnection();
//Get a savepoint from the SQL connection
return conn.setSavepoint();
}
next to make use of it…
public void commitToSave1(Savepoint sp) throws SQLException {
Connection conn = ((UnitOfWorkImpl) ((JpaEntityManager) em).getActiveSession().acquireUnitOfWork()).getAccessor().getConnection();
//roll the connection back to the savepoint
conn.rollback(sp);
//commit the current state of the transaction
conn.commit();
//clear the persistence context causing all managed entities to
//become detached
em.clear();
//commit the transaction to release locks and reset the entity
//manager
em.getTransaction().commit();
}
That concludes my brief explanation of using JPA with pessimistic locks and rollbacks for the moment for anyone who would like any more information please dont hesitate to get in touch..
Also see my original post regarding this over at http://www.nabble.com/Savepoints%2C-Checkpoints%2C-Rollbacks–td21980310.html
Custom MXJ Connector with mysql (console) and mysqldump executables
Posted by Paul Carey in Uncategorized on February 10th, 2009
Hi recently needed to setup local instances of MySQL accross clients in order to allow a copy of the database to be made on client macines so that a system could be used remotely i.e: away from the master DB server (reads only). To do this my idea was to deploy local instances of MySQL and setup replicatation so that when in contact with the master all connections would be through it and replication would ensure any updates to the master DB are copied to each client.
For this I needed some way of deploying MySQL automatically as well as setting up the replication that would be neeeded also. For this it turned out that the mxj connector by MySQL almost fitt the bill perfectly apart from the fact that it didnt come with the mysql and mysqldump executables that I needed in order to create DB dumps and imports as needed to setup the replication.
Connector mxj deploys a local instance of MySQL and can import a bundled DB by default however what I needed was to import a remote DB and setup replication. For this I made a few minor adjustments to the mxj source and bundled in the mysql (console) and mysqldump executables for Windows and Linux.
1 hour, open source and my fat head!
Posted by Paul Carey in Uncategorized on May 1st, 2008
It’s great what you can do with an hour to kill and a bit of open source kit. I’ve been wanting to get a little better with my image editing skills for a while now so took an hour out to have a little play. First I downloaded and installed the gimp and inkscape. I then set to work out finding the basics and after viewing a few video tutorials from youtube I managed to cut, trace and tweak an image of yours truelly (my fat head!).

the project – “happy returns”!
Posted by Paul Carey in Uncategorized on April 23rd, 2008
just thought id post a quick link to my latest project for uni its over @ http://paulcarey.co.uk/happy its one half of a group project the other half being a java based backend to handle bookings etc, ill try post a few screen shots at a later date. the site isn’t entirely polished but its not really meant for real use as you might tell from the heavy use of iframes!
internet explorer and standards, surelly not?
Posted by Paul Carey in web development on February 15th, 2008
well latest word is that its true IE at long last (hurray) is getting closer to being standards compliant. The latest IE 8 beta it seams his passed the acid 2 test see for yourself some milestone gee its only taken m$ 10 years and multiple releases to get there! Anyway the future may be looking better for web developers, however theres still half the world out there with broken browsers and its going to take a bit more then a working beta version to fix them.