Martin,

The OpenJPAEntityManagerSPI interface provides a method to access the
underlying JDBC connection used by the em.  You can use that connection to
get db metadata information.  Depending on whether your application uses a
container or application managed em, you may need to call em.getDelegate()
to get the underlying OpenJPA entity manager implementation.  Otherwise,
you'll get a cast exception.  Here's an example:

            OpenJPAEntityManagerSPI oem =
(OpenJPAEntityManagerSPI)em.getDelegate();
            java.sql.Connection conn = (java.sql.Connection)
oem.getConnection();
            java.sql.DatabaseMetaData dbmd = conn.getMetaData();
            String dbProductName = dbmd.getDatabaseProductName();
            String dbProductVersion = dbmd.getDatabaseProductVersion();
            String dbURL = dbmd.getURL();

I ran this code in an app configured to use a data source and it returned
the JDBC URL configured for the data source.  You could potentially parse
the URL for the host system, database name, etc.  I don't recommend using
this connection for other SQL operations since it could end up in state not
suitable for use by OpenJPA.

hth,
-Jeremy

On Thu, Jul 9, 2009 at 9:35 AM, mjdenham <mjden...@gmail.com> wrote:

>
> Hi,
>
> Is there an easy way to dynamically find the database name and schema name
> being used by an EntityManager at runtime?
>
> We are using OpenJpa 1.0.4 on Websphere and an Oracle datasource.
>
> We would like to make the information accessible on an application screen
> because we have so many environments - dev, test, uvt, uat, integration
> that
> it gets confusing to know which database a tester is using.
>
> Thanks
> Martin
> --
> View this message in context:
> http://n2.nabble.com/How-to-get-database-information-tp3231794p3231794.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Reply via email to