Friday, July 29, 2011

NetBeans7 integration with Datanucleus JDO

The most important step in developing application with DataNucleus is enhancement of compiled classes. NetBeans provides powerful features for integrating the build enviromnet for Datanucleus with out any need of plugin.

Maven :- NetBeans has a native integration with maven. Any datanucleus project based on maven will open and run as is with netbeans. No changes are needed in the project nor in netbeans.

ANT :- The default build system in netbeans is Ant. Follow through for steps involved. There are two types of enhancement. 1) When we need typesafe queries datanucleus provides annotation processor. 2)Byte Code enhancement by datanucleus enhancer.

1. Datanucleus from choose it has most of it.
2. needed if you are working with JavaEE

Setup Libraries for datanucleus
  • Datanucleus - Containing all files from lib folder
  • Datanucleusdeps - Containing all files from deps folder

TypeSafe Queries :- Datanucleus generates addtional code for supporting type safe queries. Ensure that "Enable Annotion processing" check box is selected, which is under Project Properties | build | compiling
Enhancer :- Datanucleus provides an ant task to enhancement. This task has to be executed just after compiling all the classes in the project. Open the files tab and localte the build.xml . Paste the below code before the end of  
<target name="-post-compile" depends="init">
<path id="module.enhancer.classpath">
<pathelement path="${javac.classpath}"/>
<pathelement location="${build.classes.dir}"/>
<taskdef name="datanucleusenhancer" classpathref="module.enhancer.classpath"
classname="" />
<echo message="start datanucleusenhancer"/>
<datanucleusenhancer classpathref="module.enhancer.classpath" dir="${build.classes.dir}" verbose="true">
<fileset dir="${build.classes.dir}/com/blogspot/jkook/daytrader/jdo/">
<include name="**/*.class"/>
<echo message="end datanucleusenhancer"/>

You output screen will show a log smilar as below

Compiling 5 source files to /NetBeansProjects/JDOTutorial/build/web/WEB-INF/classes
DataNucleus : JDO Query - com.blogspot.jkook.daytrader.jdo.JDOOrderData -> com.blogspot.jkook.daytrader.jdo.QJDOOrderData
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Copying 2 files to /NetBeansProjects/JDOTutorial/build/web/WEB-INF/classes
start datanucleusenhancer
Jul 29, 2011 2:40:36 PM org.datanucleus.enhancer.DataNucleusEnhancer
INFO: DataNucleus Enhancer : Using ClassEnhancer "ASM" for API "JDO"
Jul 29, 2011 2:40:37 PM org.datanucleus.enhancer.DataNucleusEnhancer main
INFO: DataNucleus Enhancer (version 3.0.0.m6) : Enhancement of classes
DataNucleus Enhancer (version 3.0.0.m6) : Enhancement of classes
Jul 29, 2011 2:40:38 PM org.datanucleus.api.jdo.metadata.JDOAnnotationReader processClassAnnotations
INFO: Class "com.blogspot.jkook.daytrader.jdo.JDOOrderData" has been specified with JDO annotations so using those.
Jul 29, 2011 2:40:38 PM org.datanucleus.metadata.MetaDataManager loadClasses
INFO: Class "com.blogspot.jkook.daytrader.jdo.QJDOOrderData" has no MetaData or annotations.
Jul 29, 2011 2:40:38 PM org.datanucleus.enhancer.AbstractClassEnhancer save
INFO: Writing class file "/NetBeansProjects/JDOTutorial/build/web/WEB-INF/classes/com/blogspot/jkook/daytrader/jdo/JDOOrderData.class" with enhanced definition
Jul 29, 2011 2:40:38 PM org.datanucleus.enhancer.DataNucleusEnhancer addMessage
INFO: DataNucleus Enhancer completed with success for 1 classes. Timings : input=514 ms, enhance=290 ms, total=804 ms. Consult the log for full details
DataNucleus Enhancer completed with success for 1 classes. Timings : input=514 ms, enhance=290 ms, total=804 ms. Consult the log for full details
end datanucleusenhancer
Created dir: /NetBeansProjects/JDOTutorial/dist
Building jar: /NetBeansProjects/JDOTutorial/dist/JDOTutorial.war
BUILD SUCCESSFUL (total time: 6 seconds)

The first red line is from the datanucleus annotation processor
Next two red lines are from the ant task we just added

Getting started with JDO on Jboss AS7 in Seven Steps

Jboss as7 is the latest JavaEE6 server while Datanucleus is the greatest peristance(JDO) implementation. Integrating these two softwares is straight forward. However to be able to leverage all the power of JavaEE and also enjoy power of JDO I will engage you in a seven step process

1.Download AS7 and Datanucleus
2.Install Jboss As 7
3.Install and configure jdbcdriver/ datasource
4.Install Datanucleus as jboss module
5.Install Datanucleus JCA
6.Configure Datanucleus
7.Access the PersistenceManager

Step 1. Download AS7 and Datanucleus
Step 2. Install Jboss AS 7
  • unzip into $home/jboss-as-7.0.0.Final -- Thats it
Jboss AS 7 is new server and its going to be a new new experice one stay with me on this wonderful flight. It loads fast <3s. Configuration is controlled by single file standalone.xml or domain.xml. Two modes of operation standalone and domain mode we will work in standalone mode. Modular server with osgi support Upgraded to JCA 1.6. Classloading is entirely fresh. Ofcource I have listed very few which are relavant here.
Start the server $home/jboss-as-7.0.0.Final/bin/ and open http://localhost:8080/
Expect to see this banner “Welcome to AS 7”. Jboss7 runs runs in two modes standalone and domain. In standalone mode it uses standalone.xml and in Domain mode it looks for domain.xml. In this exercise we are intrested in standalone.xml

Step 3. Install and configure jdbcdriver/ datasource

Very simple drop the driver in $home/jboss-as-7.0.0.Final/standalone/deployments. Yes believe me you have installed the driver and ready for use.
Next we need two datasources one Managed and other non managed. So i created java:jboss/datasources/TradeDBDS which has jta="false" and java:jboss/datasources/TradeDBDSJTA with jta="true"
Every thing in Jboss as7 is a module and controlled by respective subsystem. The datasource is managed by
Locate it in your standalone.xml. Below is the sample datasource
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasource jndi-name="java:jboss/datasources/TradeDBDS" pool-name="TRADEDB_Pool" enabled="true" jta="false" use-java-context="true" use-ccm="true">
Step 4. Install Datanucleus as jboss module
The libraries inside the webapplication cannot access the libraries in the JCA connector. Jboss takes care of JavaEE libraries such that all the webapplications have proper access. Since jdo is not part of javaEE we have to tell Jboss to share these libraries with all web applications. The trick is very easy
Create a folder $home/jboss-as-7.0.0.Final/modules/javax/jdo/main. Copy following files datanucleus-api-jdo-3.0.0-m6.jar, datanucleus-cache-3.0.0-m4.jar,datanucleus-core-3.0.0-m6.jar,datanucleus-jdo-query-3.0.0-m3.jar,datanucleus-rdbms-3.0.0-m6.jar,jdo-api-3.1-SNAPSHOT-20110319.jar.
Notice the folder main
Create a file module.xml. Contents as follows

<module name="javax.jdo" xmlns="urn:jboss:module:1.0">
<resource-root path="datanucleus-api-jdo-3.0.0-m6.jar"></resource-root>
<resource-root path="datanucleus-cache-3.0.0-m4.jar"></resource-root>
<resource-root path="datanucleus-core-3.0.0-m6.jar"></resource-root>
<resource-root path="datanucleus-jdo-query-3.0.0-m3.jar"></resource-root>
<resource-root path="datanucleus-rdbms-3.0.0-m6.jar"></resource-root>
<resource-root path="jdo-api-3.1-SNAPSHOT-20110319.jar"></resource-root>
<module name="javax.api"></module>
<module name="javax.transaction.api"></module>
Let the classlaoder majick take over

Step 5 Install Datanucleus JCA
Open the rar file remove all the jars except datanucleus-jca-3.0.0-m6.jar. Remove all xml files except plugin.xml. Edit ra.xml inside META-INF folder replace the contents with

<display-name>DataNucleus Connector</display-name>
<eis-type>JDO Adaptor</eis-type>
The important properties are PersistenceUnitName and PersistenceXmlFilename.
PersistenceUnitName has to be same across other files ie, standalone.xml and jdopersistance.xml. I am using pu. To work around an known issue we have to xmlescape the characters around pu
PersistenceXmlFilename should point to the full path.
Add a file jboss-deployment-structure.xml in META-INF folder
<module name="javax.jdo"/>
This file tells jboss to load JCA after loading datanucleus(javax.jdo)

We can access the resource-adapter via JNDI. We need to inform Jboss the JNDI location where the persistanceManagerFactory should be available. Open standalone.xml and locate the subsystem for resource-adapter and copy the below configuration.

<subsystem xmlns="urn:jboss:domain:resource-adapters:1.0">
enabled="true" use-java-context="true"
<config-property name="PersistenceUnitName">

Step 6. Configure Datanucleus
All the configuration of datanucleus is in jdopersistence.xml. The file name should be same as defined in PersistenceXmlFilename. The behaviour can be defined by adding properties. Detailed list of properties is here.
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns=""
xsi:schemaLocation="" version="1.0">
<persistence-unit name="pu">
<property name="datanucleus.connection.resourceType" value="JTA"/>
<property name="datanucleus.storeManagerType" value="rdbms"/>
<property name="datanucleus.autoCreateSchema" value="false"/>
<property name="datanucleus.ConnectionFactoryName" value="java:jboss/datasources/TradeDBDSJTA"/>
<property name="datanucleus.ConnectionFactory2Name" value="java:jboss/datasources/TradeDBDS"/>
<property name="datanucleus.autoStartClassNames" value="com.blogspot.jkook.daytrader.jdo.JDOOrderData"/>
<property name="datanucleus.autoStartMechanism" value="Classes"/>
<property name="datanucleus.autoStartMechanismMode" value="Ignored"/>
<property name="datanucleus.jtaLocator" value="custom_jndi"/>
<property name="datanucleus.jtaJndiLocation" value="java:jboss/TransactionManager"/>
datanucleus.ConnectionFactoryName has to be JTA enabled datasource
datanucleus.ConnectionFactory2Name has to be non JTA
TransactionManger JNDI name in as7 has changed so we need to configure using datanucleus.jtaJndiLocation and datanucleus.jtaLocator
Almost done ! stay with me to start coding

Step 7. Access PersistenceManager
Alright enough configuiration show me some code. PersistenceManager can be accesed through JNDI either by injecting or by direct access using InitialContext(). I prefer to use CDI @Producer and @Inject here is the sample

PersistenceManagerFactory pmf = (PersistenceManagerFactory) 
PersistenceManager pm = pmf.getPersistenceManager();

Jdk 1.6, JbossAs7, NetBeans, ojdbc6.jar, Oracle11g, Datanucleus3.0.0-m6, JavaEE6, CDI, Ejb3.1

Wednesday, July 06, 2011

Drools Best Practices - Must for every Drools programmer

A very well organised presentation with valuable suggestions.
This is a must for every drools programmer.