Difference between revisions of "EclipseLink/Examples/JPA/WLS AppScoped DataSource"

From Eclipsepedia

Jump to: navigation, search
(New page: == How to use Application Scoped Data Sources in WebLogic with EclipseLink JPA == === DataSource Definition === <source lang="xml> <?xml version = '1.0' encoding = 'windows-1252'?> <jdbc...)
 
m (1. DataSource Definition)
 
(25 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== How to use Application Scoped Data Sources in WebLogic with EclipseLink JPA ==
 
== How to use Application Scoped Data Sources in WebLogic with EclipseLink JPA ==
  
=== DataSource Definition ===
+
This how-to describes the use of application scoped data-sources within a WebLogic Application Server (version 10.3). These steps are necessary as the WebLogic container does not initialize application scoped data sources before the JPA bootstrapping is performed. Without these steps EclipseLink will be provided a null data-source and will throw an exception during its initialization.
  
<source lang="xml>
+
The configuration involves a few simple configuration steps:
 +
# Define the new data-source in a *-jdbc.xml configuration file. (EAR)
 +
# Add the JDBC module to the WebLogic Application Configuration (EAR)
 +
# Configure the JPA persistence unit to use the application scoped data source (EJB)
 +
 
 +
=== 1. DataSource Definition ===
 +
 
 +
An application scoped data source can be specified by providing a <name>-jdbc.xml file within the META-INF folder of your application's EAR. The following illustrates how the file might be defined. The important part of this configuration is the <scope>Application</scope> within the jdbc-data-source-params section.
 +
 
 +
The framework for the this <name>-jdbc.xml file can be generated by creating a globally scoped datasource on the WebLogic administration page, not associating it with a server and adding the <scope> element manually.
 +
 
 +
<source lang="xml">
 
<?xml version = '1.0' encoding = 'windows-1252'?>
 
<?xml version = '1.0' encoding = 'windows-1252'?>
 
<jdbc-data-source xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
<jdbc-data-source xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-jdbc.xsd"
+
  xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-jdbc.xsd"
xmlns="http://www.bea.com/ns/weblogic/jdbc-data-source">
+
  xmlns="http://www.bea.com/ns/weblogic/jdbc-data-source">
<name>SimpleDS</name>
+
  <name>SimpleAppScopedDS</name>
<jdbc-driver-params>
+
  <jdbc-driver-params>
<url>jdbc:oracle:thin:@localhost:1521:ORCL</url>
+
    <url>jdbc:oracle:thin:@localhost:1521:ORCL</url>
<driver-name>oracle.jdbc.OracleDriver</driver-name>
+
      <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
<properties>
+
      <properties>
<property>
+
        <property>
<name>user</name>
+
          <name>user</name>
<value>scott</value>
+
          <value>scott</value>
</property>
+
        </property>
<property>
+
        <property>
<name>servername</name>
+
          <name>servername</name>
<value>localhost</value>
+
          <value>localhost</value>
</property>
+
        </property>
<property>
+
        <property>
<name>portnumber</name>
+
          <name>portnumber</name>
<value>1521</value>
+
          <value>1521</value>
</property>
+
        </property>
<property>
+
        <property>
<name>sid</name>
+
          <name>sid</name>
<value>ORCL</value>
+
          <value>ORCL</value>
</property>
+
        </property>
</properties>
+
      </properties>
<password-encrypted>tiger</password-encrypted>
+
      <password-encrypted>pw</password-encrypted>
</jdbc-driver-params>
+
    </jdbc-driver-params>
+
 
+
    <jdbc-connection-pool-params>
<jdbc-connection-pool-params>
+
      <initial-capacity>2</initial-capacity>
<initial-capacity>2</initial-capacity>
+
      <max-capacity>10</max-capacity>
<max-capacity>10</max-capacity>
+
      <test-connections-on-reserve>true</test-connections-on-reserve>
<test-connections-on-reserve>true</test-connections-on-reserve>
+
      <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
<test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
+
    </jdbc-connection-pool-params>
</jdbc-connection-pool-params>
+
 
+
    <jdbc-data-source-params>
+
      <jndi-name>SimpleAppScopedDS</jndi-name>
<jdbc-data-source-params>
+
      <scope>Application</scope>
<jndi-name>SimpleDS</jndi-name>
+
      <global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol>
<scope>Application</scope>
+
    </jdbc-data-source-params>
<global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol>
+
</jdbc-data-source-params>
+
 
</jdbc-data-source>
 
</jdbc-data-source>
 
</source>
 
</source>
Line 52: Line 61:
 
=== WebLogic Application Configuration ===
 
=== WebLogic Application Configuration ===
  
In the EAR you will have /META-INF/weblogic-application.xml where the JDBC module is defined.
+
In the EAR you will have /META-INF/weblogic-application.xml where the JDBC module needs to be defined. This simply registers the data source defined in step 1 for use in your application.
  
 
<source lang="xml">
 
<source lang="xml">
 
<wls:module>
 
<wls:module>
<wls:name>SimpleDS</wls:name>
+
  <wls:name>SimpleAppScopedDS</wls:name>
<wls:type>JDBC</wls:type>
+
  <wls:type>JDBC</wls:type>
<wls:path>META-INF/simple-jdbc.xml</wls:path>
+
  <wls:path>META-INF/simple-jdbc.xml</wls:path>
 
</wls:module>
 
</wls:module>
 
</source>
 
</source>
Line 64: Line 73:
 
=== JPA: persistence.xml ===
 
=== JPA: persistence.xml ===
  
In the persistence.xml's definition of the persistence-unit you will need to specify an additional property so EclipseLink can lazily look up the application scoped data source.  
+
In the persistence.xml's definition of the persistence-unit you will need to specify an additional fully qualified '''"javax.persistence.jtaDataSource"''' property so EclipseLink can lazily look up the application scoped data source.
 +
This additional property '''"java:/app/jdbc/SimpleAppScopedDS"''' is the workaround for the following issue [http://bugs.eclipse.org/246126 246126].  Please retain the original <jta-data-source> element for future compatability when this workaround is no longer required.
 +
 
 +
'''Note:''' The ''jta-data-source'' and ''javax.persistence.jtaDataSource'' properties must match.
  
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="windows-1252" ?>
 
<?xml version="1.0" encoding="windows-1252" ?>
 
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
+
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
+
  version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="employee" transaction-type="JTA">
+
  <persistence-unit name="employee" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>SimpleDS</jta-data-source>
+
    <jta-data-source>java:/app/jdbc/SimpleAppScopedDS</jta-data-source>
  
<properties>
+
    <properties>
<property name="eclipselink.target-server" value="WebLogic_10" />
+
      <property name="eclipselink.target-server" value="WebLogic_10" />
<property name="javax.persistence.jtaDataSource" value="java:/app/jdbc/SimpleDS" />
+
      <property name="javax.persistence.jtaDataSource" value="java:/app/jdbc/SimpleAppScopedDS" />
</properties>
+
    </properties>
</persistence-unit>
+
 
 +
  </persistence-unit>
 
</persistence>
 
</persistence>
 
</source>
 
</source>
 +
  
 
In this example a JTA data source is being used so the property required is '''javax.persistence.jtaDataSource'''. If however you are using a non-jta-data-source then you should use the '''javax.persistence.nonJtaDataSource''' property to specify the JNDI name to use.
 
In this example a JTA data source is being used so the property required is '''javax.persistence.jtaDataSource'''. If however you are using a non-jta-data-source then you should use the '''javax.persistence.nonJtaDataSource''' property to specify the JNDI name to use.
 +
 +
==Troubleshooting==
 +
===Timeout or Data Source does not exist Error===
 +
If you get a '''java.sql.SQLException: Cannot obtain connection after 30 seconds. : java.sql.SQLException: Data Source Connection1 does not exist." in either EclipseLink, Kodo or OpenJPA then verify that your '''<driver-name>''' in your *jdbc.xml config is transactional.
 +
 +
===Password Encryption Error===
 +
If you get the following '''invalid pad byte''' exception when deploying your EAR to WebLogic 10.3 then check that you are using the latest ''AES'' encryption instead of ''3DES''.  You can regenerate your encrypted password by creating a temporary globally defined datasource and viewing the *jdbc.xml file generated on your domain config directory.
 +
<pre>
 +
Feb 20, 2009 11:46:01 AM EST> <Error> <Deployer> <BEA-149265> <Failure occurred in the execution of deployment request with ID '1235148360979' for task '20'. Error is: 'com.rsa.jsafe.JSAFE_PaddingException: Could not perform unpadding: invalid pad byte.'
 +
com.rsa.jsafe.JSAFE_PaddingException: Could not perform unpadding: invalid pad byte.
 +
at com.rsa.jsafe.JA_PKCS5Padding.a(Unknown Source)
 +
at com.rsa.jsafe.JG_BlockCipher.decryptFinal(Unknown Source)
 +
at weblogic.security.internal.encryption.JSafeEncryptionServiceImpl.decryptBytes(JSafeEncryptionServiceImpl.java:113)
 +
at weblogic.security.internal.encryption.JSafeEncryptionServiceImpl.decryptString(JSafeEncryptionServiceImpl.java:173)
 +
at weblogic.security.internal.encryption.ClearOrEncryptedService.decrypt(ClearOrEncryptedService.java:96)
 +
</pre>
 +
<source lang="xml">
 +
    <password-encrypted>{AES}...=</password-encrypted>
 +
</source>
 +
 +
==References==
 +
*See [[EclipseLink/Examples/JPA/WebLogic_Web_Tutorial|WebLogic EAR Application Tutorial]] on the JPA examples page.
 +
*Base bug for full fix in WebLogic Server release is being tracked by [http://bugs.eclipse.org/246126 246126].

Latest revision as of 17:43, 26 March 2009

Contents

[edit] How to use Application Scoped Data Sources in WebLogic with EclipseLink JPA

This how-to describes the use of application scoped data-sources within a WebLogic Application Server (version 10.3). These steps are necessary as the WebLogic container does not initialize application scoped data sources before the JPA bootstrapping is performed. Without these steps EclipseLink will be provided a null data-source and will throw an exception during its initialization.

The configuration involves a few simple configuration steps:

  1. Define the new data-source in a *-jdbc.xml configuration file. (EAR)
  2. Add the JDBC module to the WebLogic Application Configuration (EAR)
  3. Configure the JPA persistence unit to use the application scoped data source (EJB)

[edit] 1. DataSource Definition

An application scoped data source can be specified by providing a <name>-jdbc.xml file within the META-INF folder of your application's EAR. The following illustrates how the file might be defined. The important part of this configuration is the <scope>Application</scope> within the jdbc-data-source-params section.

The framework for the this <name>-jdbc.xml file can be generated by creating a globally scoped datasource on the WebLogic administration page, not associating it with a server and adding the <scope> element manually.

<?xml version = '1.0' encoding = 'windows-1252'?>
<jdbc-data-source xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-jdbc.xsd"
  xmlns="http://www.bea.com/ns/weblogic/jdbc-data-source">
  <name>SimpleAppScopedDS</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@localhost:1521:ORCL</url>
      <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
      <properties>
        <property>
          <name>user</name>
          <value>scott</value>
        </property>
        <property>
          <name>servername</name>
          <value>localhost</value>
        </property>
        <property>
          <name>portnumber</name>
          <value>1521</value>
        </property>
        <property>
          <name>sid</name>
          <value>ORCL</value>
        </property>
      </properties>
      <password-encrypted>pw</password-encrypted>
    </jdbc-driver-params>
 
    <jdbc-connection-pool-params>
      <initial-capacity>2</initial-capacity>
      <max-capacity>10</max-capacity>
      <test-connections-on-reserve>true</test-connections-on-reserve>
      <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
    </jdbc-connection-pool-params>
 
    <jdbc-data-source-params>
      <jndi-name>SimpleAppScopedDS</jndi-name>
      <scope>Application</scope>
      <global-transactions-protocol>TwoPhaseCommit</global-transactions-protocol>
    </jdbc-data-source-params>
</jdbc-data-source>

[edit] WebLogic Application Configuration

In the EAR you will have /META-INF/weblogic-application.xml where the JDBC module needs to be defined. This simply registers the data source defined in step 1 for use in your application.

<wls:module>
  <wls:name>SimpleAppScopedDS</wls:name>
  <wls:type>JDBC</wls:type>
  <wls:path>META-INF/simple-jdbc.xml</wls:path>
</wls:module>

[edit] JPA: persistence.xml

In the persistence.xml's definition of the persistence-unit you will need to specify an additional fully qualified "javax.persistence.jtaDataSource" property so EclipseLink can lazily look up the application scoped data source. This additional property "java:/app/jdbc/SimpleAppScopedDS" is the workaround for the following issue 246126. Please retain the original <jta-data-source> element for future compatability when this workaround is no longer required.

Note: The jta-data-source and javax.persistence.jtaDataSource properties must match.

<?xml version="1.0" encoding="windows-1252" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="employee" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:/app/jdbc/SimpleAppScopedDS</jta-data-source>
 
    <properties>
      <property name="eclipselink.target-server" value="WebLogic_10" />
      <property name="javax.persistence.jtaDataSource" value="java:/app/jdbc/SimpleAppScopedDS" />
    </properties>
 
  </persistence-unit>
</persistence>


In this example a JTA data source is being used so the property required is javax.persistence.jtaDataSource. If however you are using a non-jta-data-source then you should use the javax.persistence.nonJtaDataSource property to specify the JNDI name to use.

[edit] Troubleshooting

[edit] Timeout or Data Source does not exist Error

If you get a java.sql.SQLException: Cannot obtain connection after 30 seconds. : java.sql.SQLException: Data Source Connection1 does not exist." in either EclipseLink, Kodo or OpenJPA then verify that your <driver-name> in your *jdbc.xml config is transactional.

[edit] Password Encryption Error

If you get the following invalid pad byte exception when deploying your EAR to WebLogic 10.3 then check that you are using the latest AES encryption instead of 3DES. You can regenerate your encrypted password by creating a temporary globally defined datasource and viewing the *jdbc.xml file generated on your domain config directory.

Feb 20, 2009 11:46:01 AM EST> <Error> <Deployer> <BEA-149265> <Failure occurred in the execution of deployment request with ID '1235148360979' for task '20'. Error is: 'com.rsa.jsafe.JSAFE_PaddingException: Could not perform unpadding: invalid pad byte.'
com.rsa.jsafe.JSAFE_PaddingException: Could not perform unpadding: invalid pad byte.
	at com.rsa.jsafe.JA_PKCS5Padding.a(Unknown Source)
	at com.rsa.jsafe.JG_BlockCipher.decryptFinal(Unknown Source)
	at weblogic.security.internal.encryption.JSafeEncryptionServiceImpl.decryptBytes(JSafeEncryptionServiceImpl.java:113)
	at weblogic.security.internal.encryption.JSafeEncryptionServiceImpl.decryptString(JSafeEncryptionServiceImpl.java:173)
	at weblogic.security.internal.encryption.ClearOrEncryptedService.decrypt(ClearOrEncryptedService.java:96)
    <password-encrypted>{AES}...=</password-encrypted>

[edit] References