org.apache.myfaces.extensions.cdi.jpa.api.datasource
Interface DataSourceConfig

All Superinterfaces:
CodiConfig, Serializable

public interface DataSourceConfig
extends CodiConfig

Configuration for the DataSource.

If you use the ConfigurableDataSource then this interface needs to be implemented in customer projects to return the proper values to connect to the database.

The connectionId parameter can be used to distinguish between different databases.

There are 3 ways to configure a DataSource

  1. via JNDI lookup - specify the JNDI resource location for the DataSource via getJndiResourceName(String)
  2. via a DataSource class name plus properties - This will be used if getJndiResourceName(String) returns null. In this case you must specify the getConnectionClassName(String) to contain the class name of a DataSource, e.g. "";com.mchange.v2.c3p0.ComboPooledDataSource""; and return additional configuration via getConnectionProperties(String).
  3. via a JDBC Driver class name plus properties - This will be used if getJndiResourceName(String) returns null. In this case you must specify the getConnectionClassName(String) to contain the class name of a javax.sql.Driver, e.g. "";org.hsqldb.jdbcDriver""; and return additional configuration via getConnectionProperties(String).

Usage

Instead of configuring any hardcoded DataSource provider, JDBC driver or JNDI location of the DataSource you just configure our ConfigurableDataSource in your persistence.xml. This class is an implementation of DataSource and acts as kind of a proxy to determine the underlying database configuration for your usage scenarios.

A possible persistence.xml configuration would look like the following:

 <persistence xmlns="http://java.sun.com/xml/ns/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">

     <persistence-unit name="test" >
         <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>

         <class>org.apache.myfaces.extensions.cdi.jpa.test.TestEntity</class>

         <properties>
             <property name="openjpa.ConnectionDriverName"
                  value="org.apache.myfaces.extensions.cdi.jpa.impl.datasource.ConfigurableDataSource"/>
             <property name="openjpa.ConnectionProperties"
                  value="connectionId=core"/>
         </properties>

     </persistence-unit>
 </persistence>
 


Method Summary
 String getConnectionClassName(String connectionId)
           
 Properties getConnectionProperties(String connectionId)
           
 String getJdbcConnectionUrl(String connectionId)
          This will only get used if getConnectionClassName(String) is a javax.sql.Driver.
 String getJndiResourceName(String connectionId)
          Return the JNDI resource name if the DataSource should get retrieved via JNDI.
 

Method Detail

getJndiResourceName

String getJndiResourceName(String connectionId)
Return the JNDI resource name if the DataSource should get retrieved via JNDI. If a native JDBC connection should get used, this method must return null. And the JDBC connection properties must get set via getConnectionClassName(String) and getConnectionProperties(String).

Parameters:
connectionId - used to distinguish between different databases.
Returns:
the JNDI lookup for the DataSource or null if a native JDBC connection should get used.

getConnectionClassName

String getConnectionClassName(String connectionId)
Parameters:
connectionId - used to distinguish between different databases.
Returns:
the fully qualified class name of the JDBC driver for the underlying connection or null if getJndiResourceName(String) is not being used

getConnectionProperties

Properties getConnectionProperties(String connectionId)
Parameters:
connectionId - used to distinguish between different databases.
Returns:
allows to configure additional connection properties which will get applied to the underlying JDBC driver or null if getJndiResourceName(String) is not being used

getJdbcConnectionUrl

String getJdbcConnectionUrl(String connectionId)
This will only get used if getConnectionClassName(String) is a javax.sql.Driver. Foor Datasources, the underlying connection url must get configured via getConnectionProperties(String).

Parameters:
connectionId - used to distinguish between different databases.
Returns:
the connection url, e.g. "jdbc://..." or null if getJndiResourceName(String) is not being used


Copyright © 2010-2012 The Apache Software Foundation. All Rights Reserved.