| MS Windows users : there is some problem with generated class due to case insensitive on windows. If you found NoClassDefFound exception then you should check generated class names against java filenames |
| current version is 1.0.10 and is available from our public maven repository http://top.touk.pl/maven2/release/pl/touk/top/maven-jpub-plugin/ |
Introduction
Maven2 plugin that integrates Oracle JPublisher into Maven project lifecycle.
It generates Java classes with methods for every stored procedure you specify from your Oracle Database,
and you do not have to worry about mapping types, including complex types, lists etc..
License
As all our top.touk.pl projects its based on ASF License 2.0 :
/* * Copyright (c) 2008-2010 TouK.pl * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
Requirments
JPublisher artifacts.
First you have to install JPublisher artifacts in your M2 repository.
So download jpublisher distribution from here and install it your favorite way in your repository.
I have installed them as :
<dependency> <groupId>oracle</groupId> <artifactId>runtime12</artifactId> <version>10.2</version> </dependency> <dependency> <groupId>oracle</groupId> <artifactId>translator</artifactId> <version>10.2</version> </dependency>
Oracle Driver
JPublisher generated classes work only with unwrapped JDBC Driver. This is some kind of problem if you are using for example WebLogic datasource (but as Oracle has bought WebLogic recently, this problem should disappear in near future).
For now you have configure your own datasource using OracleDataSource included in oracle driver.
I made jdbc driver available in my repository as :
<dependency> <groupId>oracle</groupId> <artifactId>ojdbc5</artifactId> <version>11.1.0.6.0</version> </dependency> <dependency> <groupId>oracle</groupId> <artifactId>orai18n</artifactId> <version>11.1.0.6.0</version> </dependency>
Plugin usage
To use the plugin you just need to add following configuration to you pom.xml:
... <build> ... <plugins> ... <plugin> <groupId>pl.touk.top</groupId> <artifactId>maven-jpub-plugin</artifactId> <version>1.0.9</version> <configuration> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <user>${database.user}/${database.password}</user> <url>${database.url}</url> <sourceDirectory>${project.build.directory}/generated-sources/src</sourceDirectory> <genPackage>pl.touk.myexample.dao.oracle</genPackage> <encoding>UTF-8</encoding> <compile>true</compile> <connscope>method</connscope> <toString>true</toString> <other>-omit_schema_names -datasource=true -connscope=method -compatible=9i -numbertypes=oracle -input=${project.basedir}/src/main/resources/jpub_object_list</other> <debug>true</debug> <closeConnection>true</closeConnection> <closeInvalidConnection>true</closeInvalidConnection> <xmx>256m</xmx> <xms>256m</xms> <skip>${skipJPub}</skip> </configuration> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>unpack</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>oracle</groupId> <artifactId>ojdbc5</artifactId> <version>11.1.0.6.0</version> </dependency> <dependency> <groupId>oracle</groupId> <artifactId>orai18n</artifactId> <version>11.1.0.6.0</version> </dependency> <dependency> <groupId>oracle</groupId> <artifactId>runtime12</artifactId> <version>10.2</version> </dependency> <dependency> <groupId>oracle</groupId> <artifactId>translator</artifactId> <version>10.2</version> </dependency> <dependency> <groupId>ant</groupId> <artifactId>ant</artifactId> <version>1.6.5</version> </dependency> <dependency> <groupId>ant</groupId> <artifactId>ant-optional</artifactId> <version>1.5.2</version> </dependency> </dependencies> </plugin> ... </plugins> ... </build>
That will generate Java classes for stored procedures listed in -input=${project.basedir}/src/main/resources/jpub_object_list
All parameters specified in <configuration> section are one to one copy of those you can find in JPublisher documentation.
The only exception is <closeConnection> parameter. Setting it to true will modify generated classes so that after executing any stored procedure used connection will be closed - meaning it will be returned to the pool!
Datasource configuration
If you are using Spring you can make it as simply as :
<bean name="dataSource" class="oracle.jdbc.pool.OracleDataSource"> <property name="URL" value="${database.url}"/> <property name="user" value="${database.user}"/> <property name="password" value="${database.password}"/> <property name="connectionCachingEnabled" value="true"/> <!--<property name="connectionCacheName" value="MYEXAMPLE_CACHE"/>--> <property name="implicitCachingEnabled" value="true"/> <property name="connectionCacheProperties"> <props> <prop key="MinLimit">5</prop> <prop key="ConnectionCachingEnabled">True</prop> <prop key="InitialLimit">5</prop> <prop key="MaxLimit">5</prop> <prop key="ValidateConnection">false</prop> <!--<prop key="InactivityTimeout">10</prop>--> <prop key="AbandonedConnectionTimeout">180</prop> <prop key="ConnectionWaitTimeout">60</prop> <!--<prop key="TimeToLiveTimeout">5</prop>--> <!--<prop key="PropertyCheckInterval">300</prop>--> </props> </property> </bean>
Using generated classes
Oracle decided to make these generated classes NOT thread safe,
but with a little trick from Spring you can easily overcome this problem.
Spring code snippet below assumes that I have class pl.touk.myexample.dao.oracle.MyExampleStoredProceduresPackage. Generated by JPublisher that contains methods to my stored procedures.
To use it as a bean in spring just do :
<bean id="targetMyExample" class="pl.touk.myexample.dao.oracle.MyExampleStoredProceduresPackage" scope="prototype"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="threadTargetMyExample" class="org.springframework.aop.target.ThreadLocalTargetSource"> <property name="targetBeanName" value="targetMyExample"/> </bean> <bean id="myExample" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="targetSource" ref="threadTargetMyExample"/> </bean>
Working with Oracle DataSource pool
If you are using datasource pool please remeber about these two close* parameters
... <build> ... <plugins> ... <plugin> <groupId>pl.touk.top</groupId> <artifactId>maven-jpub-plugin</artifactId> <version>1.0.9</version> <configuration> ... <closeConnection>true</closeConnection> <closeInvalidConnection>true</closeInvalidConnection>
here is documentation for them form source code :
/**
* If this parameter is set to true closeConnection() method is called in every finally block. So if you are using the pool logical connection will be closed and returned to the pool.
*
* Defeault to true;
* @parameter
*/
private Boolean closeConnection;
/**
* If this parameter is set to true closeInvalidConnection() method is called in every catch SQLException block.
* It marks used connections as invalid so that underlying physical connection will also be closed and removed from the pool.
* So if you expect lots of SQLException you may kill the database.
* It works only if closeConnection is set to true too!!!
*
* Defeault to true;
* @parameter
*/
private Boolean closeInvalidConnection;
I know this is not perfect, but it is good enough and it works very well.
Good luck.
Comments (2)
Oct 01, 2009
Piotr Swiecicki says:
Hi guys Nice stuff, I would like to use maven-jpub-plugin in one of my project. ...Hi guys
Nice stuff, I would like to use maven-jpub-plugin in one of my project. It might be dumb question, but what is the licence of this project? I know it is here in 'Home of TouK Open Source Projects' section, but can't find any licensing information on your site. Is it GPL or any other FOSS?
Thanks
Oct 03, 2009
Kuba Nowakowski says:
All of our projects are on ASL 2.0 as should be stated in all files.All of our projects are on ASL 2.0 as should be stated in all files.