After setting up a data source in your AppServer, e.g. for WebLogic Server, you want to use it in your application with JPA.

Basically there are three ways to connect to a db with JPA:

  • JDBC data source with JTA transactions (not supported by a Servlet-Container like Tomcat)
  • JDBC data source with self-managed transactions, i.e. transaction-type=”RESOURCE_LOCAL”
  • direct JDBC connection

The first and second are well discussed in the www. So we give an example on the second with provider Hibernate:

<persistence-unit name="PU_ResourceLocal_WithDS" transaction-type="RESOURCE_LOCAL">

  <!-- the JNDI name as specified in your AppServer -->

    <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
    <property name="hibernate.max_fetch_depth" value="5" />

For Tomcat 6 and EclipseLink this is well explained in http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial.

, , , , , ,


To use a data source in WLS (e.g. to benefit from the built-in connection pooling, XA transactions etc.) you have to configure it from the WLS admin console.
Prior to this post, we installed the Oracle DB and Oracle WLS.

  1. start the admin console, e.g. http://localhost:7001/console
  2. in the left tree menu named ‘Domain Structure’ go to ‘Services’ -> ‘JDBC’ -> ‘Data Sources’
  3. click ‘New’ and enter your data:
    • name:     mh_JDBC_DS_orcl (just a unique name in WLS to identify your ds)
    • JNDI Name:     jdbc/mh_orcl
    • Database Type:    Oracle
    • Database Driver:     oracle.jdbc.xa.client.OracleXADataSource (use the XA version if your App participates in global/distributed transactions)
    • Database Name:     orcl (the name you specified in the db installation)
    • Host Name:     localhost (or your IP where the db listens)
    • Port:     1521 (port where the db listens)
    • Database User Name:  mh (user to login)
    • Password:     <pwd>
    • under ‘Targets’: check the server, e.g. ‘AdminServer’ where you want to use this ds
  4. You can explore your settings also on the file system:
    • C:\Oracle\Middleware\user_projects\domains\mh_domain\config\jdbc\mh_JDBC_DS_orcl-5723-jdbc.xml, and an entry to this file in
    • C:\Oracle\Middleware\user_projects\domains\mh_domain\config\config.xml

Alternatively, if you need to make multiple installations/configurations, there is the possibility to use the silent installation and WLST (WebLogic Scripting Tool).

Now you can call the ds in your application via JNDI or if you use JPA you need to specify the JNDI name in persistence.xml.

, , , ,


If you want to install a library ‘C:\path\io-1.3.jar’ manually in your local repository, just issue the following command:

mvn install:install-file -Dfile=C:\path\io-1.3.jar -DgroupId=org.bigdev -DartifactId=io -Dversion=1.3 -Dpackaging=jar

You can now reference this jar in your pom.xml file as



If you use JPA with Hibernate, CDI with Weld, then the default Tomcat PermGen (permanent Generation) size is soon becoming too small – already for dev purposes without user load…

E.g., if you have 2GB of RAM, reserve 1GB for your OS, you could give a dedicated Tomcat server about 1GB:

If you’re using the startup.bat, append following line in catalina.bat in front of JAVA_OPTS:
-Xms1024m -Xmx1024m -XX:MaxPermSize=128m

set JAVA_OPTS=%JAVA_OPTS% -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties" -Xms1024m -Xmx1024m -XX:MaxPermSize=128m

You also could add a new environment variable called JAVA_OPTS with value -Xms1024m -Xmx1024m -XX:MaxPermSize=128m

I also was in the situation of a Win XP machine with 512MB of RAM, where I set -Xms256m -Xmx256m -XX:MaxPermSize=64m. There could not run any more processes without starting to page… There also ran a HSQLDB, and sometimes I opened Eclipse together with Firefox :-)

, ,


When you need to retrieve the plain Connection from JPA, unfortunately, there is no JPA standard way (unless you get a configured datasource with JNDI). This is needed when you use libraries or legacy applications which use JDBC instead of JPA.

Hibernate 3.x and JPA 1.0

With Hibernate you can get the org.hibernate.session. In pre Hibernate 4.o there was the connection() method which now is removed.

EntityManager em = ...;
Session session = (Session) em.getDelegate();
Connection conn = session.connection();

Hibernate 3.x and JPA 2.0

EntityManager em = ...;
Connection conn = em.unwrap(Session.class).connection();

Hibernate 4.x and JPA 2.0

Session session = em.unwrap(Session.class);
SessionFactoryImplementor sfi = (SessionFactoryImplementor) session.getSessionFactory();
ConnectionProvider cp = sfi.getConnectionProvider();
Connection conn = cp.getConnection();


If you are running inside a container, you could also perform a JNDI lookup on the configured DataSource.

, , , ,


For the use of JSF you only need a servlet container (no full application server).

JSF 1.2, 2.0: Servlet 2.5 / JSP 2.1 container

JSF 1.2 and JSF 2.0 depend only on Servlet 2.5 / JSP 2.1 containers. Note that EL 2.1 is a component of the JSP 2.1 Specification. This is the case in JEE 5 AppServers:

  • Tomcat 6
  • Glassfish 2
  • JBoss AS 5
  • Oracle WebLogic 11g
  • IBM WebSphere 7, etc.

E.g. So you can use JSF 2.0 in Tomcat 6, but not the features of EL 2.2, like call JSF action method with arguments. To tune Tomcat 6 towards Java EE 6 see here.

JSF 2.1, 2.2: Servlet 3.0 / JSP 2.2 container

JSF 2.1 and JSF 2.2 depend on Servlet 3.0 / JSP 2.2 containers. Note that EL 2.2 is a component of the JSP 2.2 Specification. This is the case in JEE 6 AppServers:

  • Tomcat 7
  • Glassfish 3
  • JBoss AS 6
  • Oracle WebLogic 12g
  • IBM WebSphere 8
  • or newer versions

For an overview of JEE 6 compatible servers: http://www.oracle.com/technetwork/java/javaee/overview/compatibility-jsp-136984.html
What’s new in JSF 2.1? It’s mainly a maintenance release:
http://it-republik.de/jaxenter/news/Was-ist-neu-in-JSF-2.1-057653.html (German)

, , , , , , , , , , , ,


Unfortunately, there are no functions in EL concerning Strings. This may be of interest when you want to display different data than delivered from a backing bean, and provide the logic in the logic in the view (pro: can be easily modified, con: logic in the view?). There are several ways (client-side with JavaScript/server-side with Java), but I want to show how to use (already existing) JSTL functions inside EL Expression without writing any extra Java nor JavaScript code:

  • Add JSTL functions namespace in Facelets XHTML:


  • JSTL function startsWith inside EL expression


  • JSF Example: filtering values starting with a prefix:

<h:outputText value="#{fn:startsWith(beanName.fieldName,'PREFIX_')} ? '' : beanName.fieldName "/>

The other JSTL functions are, see JSTL Functions API documentation: http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html

  • toUpperCase, toLowerCase: Changes the capitalization of a string
  • substring, substringBefore, substringAfter: Gets a subset of a string
  • trim: Trims white space from a string
  • replace: Replaces characters in a string
  • indexOf, startsWith, endsWith, contains, containsIgnoreCase: Checks whether a string contains another string
  • split: Splits a string into an array
  •  join: Joins a collection into a string
  • escapeXml: Escapes XML characters in a string
  • length: Returns the number of items in a collection, or the number of characters in a string

, , ,


If you have to deploy to several environments, like DEV, TEST, PROD etc., and you don’t want to edit the config and property files (e.g. db url connect string, data source names, logging props, etc.) over and over again, then maybe it is time :

  1. for each environment have a separate version of the config / property files.
  2. Dependent on the environment, load the right version.

For 2. you need some kind of environment detection. There are a lot of ways of achieving this:


If you have a WebApp then you could provide a context parameter:

  <description>Environment name for dependent configuration/description>


ServletContext servletContext = ...;
String env = servletContext.getInitParameter("org.bigdev.env");
if ("DEV".equals(env)) {
    // DEV
} else if ("TEST".equals(env)) {
    // TEST
} else if ("PROD".equals(env)) {
    // PROD

There you trade editing one property, instead of many in different files.

Property File or just a empty Trigger File

if ((new File("WEB-INF/env/DEV.trigger")).exists()) {
    // DEV
} else if ((new File("WEB-INF/env/TEST.trigger")).exists()) {
    // TEST
} else if ((new File("WEB-INF/env/PROD.trigger")).exists()) {
    // PROD

Host Name

String hostName = InetAddress.getLocalHost().getHostName();
if ("localhost".equals(hostName)) {
    // DEV
} else if ("srv007".equals(hostName)) {
    // TEST
} else if ("srv008".equals(hostName)) {
    // PROD

IP address or range

String ip = InetAddress.getLocalHost().getHostAddress();
if ("".equals(ip)) {
    // DEV
} else if (ip.startsWith("2.3.4.")) {
    // TEST
} else if (ip.startsWith("2.3.5.")) {
    // PROD

Operating System

String osName = System.getProperty("os.name");
if (osName.startsWith("Windows")) {
    // DEV
} else if (osName.startsWith("Linux")) {
    // TEST
} else if (osName.startsWith("AIX")) {
    // PROD

System Property

String env = System.getProperty("org.bigdev.env");
if ("DEV".equals(env)) {
    // DEV
} else if ("TEST".equals(env)) {
    // TEST
} else if ("PROD".equals(env)) {
    // PROD

Or any other system property that identifies your env.

There are many more use cases of the knowledge of the environment other than config/property files. You can en-/disable certain behavior/features of your application dependent on the current environment. E.g. in JPA overwrite the annotation object-relational mapping via orm.xml dependent on the env, see  here.

, ,


Meanwhile annotations are preferrred to XML configuration in Java EE. One still can use XML to override annotations. The same is true in JPA: one configures the object-relational mapping via entity annotations. I was in the situtation to make some mappings of the JPA entities and the database tables configurable, when deploying in different environments.

In my situation I needed to switch the ID generation strategy, auto vs. sequence, in all 30+ entites back and forth (building up different database structures for different environments in the same project is a bad thing, but happens in real life…). Either I could change the annotations in each entity file, like I did a few times, or:

  1. Specify with annotations the standard behavior.
  2. Override the annotations with orm.xml.

I will give an example to my situation. In the entity I specified the sequence generation via annotations:

@SequenceGenerator(name="TABLENAME_ID_GENERATOR", sequenceName="SEQUENCENAME_IN_DB")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TABLENAME_ID_GENERATOR")
private Long id;

If you start overwriting a property, all annotations on the given property are ignored. So you also have to specify e.g. the @Column-configuration in the xml:
So to overwrite this with strategy=GenerationType.AUTO:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings  version="1.0"
  xsi:schemalocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">

  <description>Override Annotations...</description>

  <!-- Set the default to FIELD-access, if you like -->

      <id name="id">
        <column name="TABLE_ID" unique="true" nullable="false" precision="20"/>
        <generated-value strategy="AUTO" />


You are done if you use the default location for this xml, beside the persistence.xml: META-INF/orm.xml.

If you want another name or another location you can register this file in the persistence unit in the persistence.xml:
We rename the file idGenerationStrategyAUTO.xml and then have to specify the mapping-file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
  xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="bigDevPersistenceUnit_DEV" transaction-type="RESOURCE_LOCAL">



Finally, in the sense of “continous delivery” you could declare different persistence units, e.g. bigDevPersistenceUnit_DEV, bigDevPersistenceUnit_PROD, and create the right entity manager factory by using the detected environment DEV, PROD, see here.

, , ,


After installing the database, I’m getting the WebLogic Server 11gR1 (10.3.3). You have to accept again the same “OTN License Agreement”.

Installation of WLS

Download and unzip

1. At the download page of Oracle WebLogic Server  you can choose versions for different platforms (32/64 bit). Although I would prefer the 64 bit version I’m sticking to the 32 bit version, due to the easy Windows Installer (double-click exe, and follow a wizard…) – I don’t want to invest too much time in the installation.
I choose the full package with Oracle Coherence and Oracle Enterprise Pack for Eclipse, although I won’t need Coherence but want to check out the OEPE (there’s also a zip with AppServer only, but the readme reveals no exe-installation with manual configuration – unfortunately WLS installation isn’t as easy as JBoss, Glassfish: unzip, done). So download

  • wls1033_oepe111150_win32.exe (about 0,9 GB)

Optional download: Documentation package E14571_01.zip, if you want offline all the WLS 11gR1 oracle docs in html and pdf version.


2. Double-click wls1033_oepe111150_win32.exe, and wait for preparation of installation, about 5 min.

3. Follow the wizard:

  • Create a “Middleware Home Directory”, I choose the suggested one C:\Oracle\Middleware (in C:\Oracle also lies the db)
  • choose “Typical” installation (vs. “Custom”)
  • accept the suggested installation directories:
  • WebLogic Server: C:\Oracle\Middleware\wlserver_10.3
  • Oracle Coherence: C:\Oracle\Middleware\coherence_3.5
  • Oracle Enterprise Pack for Eclipse: C:\Oracle\Middleware\oepe_11gR1PS2
  • Create shortcuts in start menu for “all users” as recommended
  • Check installation summary, click “Next” to start the actual installation, wait about 20 min., time for a coffee :-)

4. Check “Run Quickstart” and press “Done” to create and configure a WLS domain:

  • in Quickstart, select “Getting started with WebLogic Server 10.3.3″
  • next select “Create a new WebLogic domain”
  • Select to “Generate a domain configured automatically to support the following products:” and check JAX-RPC- and JAX-WS extensions
  • enter domain name and location: I choose “mh_domain” and C:\Oracle\Middleware\user_projects\domains
  • configure the administrator user name and password: I choose “admin” and some easy to remember pwd (it has to be longer than 8 chars)
  • choose “Development Mode” and Sun SDK for getting the user/pwd out if boot.properties and poll for apps to deploy (auto-deploy)
  • check no further configurations, inspect the summary and click “Next”, installation complete!


Starting and Stopping WLS


Visit the admin server http://localhost:7001/console and log in as the admin as specified before.

, , ,