Posts Tagged ‘JDBC’
09
Mar

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">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>

  <!-- the JNDI name as specified in your AppServer -->
  <non-jta-data-source>jdbc/mh_orcl</non-jta-data-source>

  <properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
    <property name="hibernate.max_fetch_depth" value="5" />
  </properties>
</persistence-unit>

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

, , , , , ,

14
Feb

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();

JNDI-Lookup

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

, , , ,

18
Jan

To install and run a HSQLDB in server mode proceed as follows:

  • Download the release from http://hsqldb.org.
  • Unzip it, e.g. C:\hsqldb-2.2.5\.
  • The HSQLDB-jar is then found at C:\hsqldb-2.2.5\hsqldb\lib\hsqldb.jar.
  • Create a batch file for starting the HSQLDB in server mode, e.g. C:\hsqldb-2.2.5\hsqldb\bin\hsqldbServer_start.bat with content:

@java -classpath ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:../data/MH --dbname.0 MH_DB

  • This creates and starts a db with name MH_DB with db-files in the directory C:\hsqldb-2.2.5\hsqldb\data. You can start several databases at once, replace the batch file content with:

@java -classpath ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:../data/MH1 --dbname.0 MH_DB1 --database.1 file:../data/MH2 --dbname.1 MH_DB2

  • You then can reach the HSQLDB via JDBC / JPA

driver: 'org.hsqldb.jdbcDriver'
url: 'jdbc:hsqldb:hsql://localhost/MH_DB'
user: 'SA'
pwd: ''

  • When using JPA with Hibernate then you can specify a dialect in persistence.xml (for other JPA providers, check the documentation):

<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />

, , , , , ,