Class DataSourceConnectionProvider

java.lang.Object
org.openxava.util.DataSourceConnectionProvider
All Implemented Interfaces:
Serializable, IConnectionProvider

public class DataSourceConnectionProvider extends Object implements IConnectionProvider, Serializable
Adapter from JNDI DataSource interface to IConnectionProvider interface.
Author:
Javier Paniza
See Also:
  • Constructor Details

    • DataSourceConnectionProvider

      public DataSourceConnectionProvider()
  • Method Details

    • createByComponent

      public static IConnectionProvider createByComponent(String componentName)
      Throws:
      XavaException
    • getCleanJPADataSourceName

      public static String getCleanJPADataSourceName()
      Returns the clean JNDI name of the default JPA data source (without 'java:comp/env/' prefix).
      Since:
      8.0
    • getDefaultCleanJPADataSourceName

      public static String getDefaultCleanJPADataSourceName()
      Returns the clean JNDI name of the default JPA data source (without 'java:comp/env/' prefix), reading it directly from persistence.xml without triggering Hibernate initialization.
      Since:
      8.0
    • getByComponent

      public static IConnectionProvider getByComponent(String componentName)
      Throws:
      XavaException
    • getDataSource

      public DataSource getDataSource() throws NamingException
      DataSource to wrap
      Throws:
      NamingException
    • getDataSourceJNDI

      public String getDataSourceJNDI()
      JNDI of DataSource to wrap.

      Only works if datasource == null

    • setDataSourceJNDI

      public void setDataSourceJNDI(String dataSourceJDNI)
      JNDI of DataSource to wrap.

      Only works if datasource == null

    • getConnection

      public Connection getConnection() throws SQLException
      Returns a database connection.

      By default the connection is obtained from the DataSource configured in JNDI. If setUseHibernateConnection(boolean) is set to true, the connection is obtained directly from JPA/Hibernate.

      Since version 8.0: If there is an active manager in XPersistence.getManager(), this method uses its connection instead of creating a new one. This avoids lock issues with Hibernate 7 when the connection is closed by legacy code that does not expect a Hibernate-managed connection. In this case, calling close() on the returned connection has no effect because the connection will be closed automatically when XPersistence.commit() is called at the end of OpenXava's action cycle.

      Specified by:
      getConnection in interface IConnectionProvider
      Throws:
      SQLException - If there are problem obtaining the connetion
    • getConnection

      public Connection getConnection(String dataSourceName) throws SQLException
      Description copied from interface: IConnectionProvider
      Returns a JDBC connection from a identifier.

      Specified by:
      getConnection in interface IConnectionProvider
      Parameters:
      dataSourceName - Name of data source from what I obtain the connection
      Throws:
      SQLException - If there are problem obtaining the connetion
    • setPassword

      public void setPassword(String password)
      Description copied from interface: IConnectionProvider
      Sets password used to create connection.

      * Must to call to IConnectionProvider.setUser(java.lang.String) too. Although set user and password is not mandatory.

      Specified by:
      setPassword in interface IConnectionProvider
    • setUser

      public void setUser(String user)
      Description copied from interface: IConnectionProvider
      Sets the user used to craete the connection.

      It is not mandatory to call this method.

      Specified by:
      setUser in interface IConnectionProvider
    • getPassword

      public String getPassword()
    • getUser

      public String getUser()
    • setDefaultDataSource

      public void setDefaultDataSource(String dataSourceName)
      Description copied from interface: IConnectionProvider
      Sets the datasource name used when using IConnectionProvider.getConnection().
      Specified by:
      setDefaultDataSource in interface IConnectionProvider
    • isUseHibernateConnection

      public static boolean isUseHibernateConnection()
      If true then all intances use hibernate connection for obtain connection, instead of data source connection pool.

      Useful for using outside an application server, for example, in a junit test.

    • setUseHibernateConnection

      public static void setUseHibernateConnection(boolean useHibernateConnection)
      If true then all intances use hibernate connection for obtain connection, instead of data source connection pool.

      Useful for using outside an application server, for example, in a junit test.