Wednesday, March 5, 2014

I am a happy boy...


I got all of the pieces worked out on my app running on Apache TomEE.

Before you invest too much time reading - this is really dry reading. But, if you are planning on connecting to a MySQL database from an app running on TomEE it might help you get it working.

You're still here! Well...

It took a bit longer than I had expected it to (of course I didn't get to work on it full time). But it works!

I've got my JSP sending data to my servlet using AJAX. The servlet is properly parsing the XML and uses JPA to access my MySQL database.

Perhaps strangely enough, the biggest headache that I ran into was getting the data source properly configured.

Here is what it took...

In the WEB-INF directory is my resources.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Resource id="pInit" type="DataSource">
ignoreDefaultValues true
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://ksdb:3306/projectInit
UserName username
Password password
defaultAutoCommit true
jtaManaged true

In the META-INF directory is the persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="" version="1.0"> <persistence-unit transaction-type="JTA" name="pilCommon"> <jta-data-source>pInit</jta-data-source> <class>com.pubint.projInit.entity.Project</class> <properties> <property name="openjpa.jdbc.DBDictionary" value="mysql" /> <property name="openjpa.AutoDetach" value="close" /> <property name="openjpa.DetachState" value="fetch-groups(AccessUnloaded=true)" /> <property name="openjpa.Multithreaded" value="true" /> <property name="openjpa.TransactionMode" value="managed" /> <property name="openjpa.NontransactionalRead" value="true" /> <property name="openjpa.RestoreState" value="all" /> <property name="openjpa.jdbc.SynchronizeMappings" value="false" /> <property name="openjpa.InverseManager" value="true" /> </properties> </persistence-unit> </persistence>

I might have gone overboard on the properties that I set in the persistence file but the minimal version that I modeled after from the TomEE site did not work. TomEE kept trying to use HSQL to connect to the database rather than MySQL (which is what it really should have been).

But that is okay - it works and I can finally move on to building the real pages and implementing the needed functionality.