Getting Database Connection in Pure JPA Setup

Getting Database connection in pure JPA setup

Where you want to get that connection is unclear. One possibility would be to get it from the underlying Hibernate Session used by the EntityManager. With JPA 1.0, you'll have to do something like this:

Session session = (Session)em.getDelegate();
Connection conn = session.connection();

Note that the getDelegate() is not portable, the result of this method is implementation specific: the above code works in JBoss, for GlassFish you'd have to adapt it - have a look at Be careful while using EntityManager.getDelegate().

In JPA 2.0, things are a bit better and you can do the following:

Connection conn = em.unwrap(Session.class).connection();

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

Connection getting closed while executing long run query through JPA TransactionTemplate

Try by increasing the removeAbandonedTimeout value, currently it is set to 420 seconds (7 minutes). Increase this value more than 30 minutes(in seconds). A connection is considered abandoned and eligible for removal if it has not been used for longer than removeAbandonedTimeout.

Accessing external database from a Spring application

  • How can projectB know about beans in projectA?

By adding the beans in projectA as dependency to projectB.

  • Can i access dbA with JPA(Hibernate) from projectB?

Yes you can.

  • Now i am accessing to dbA from projectB with JDBC and explicit SQL queries. But i need to entityManager.merge entityManager.save and so. Is it possible?

Yes, it is possible after you add JPA related ocde to projectB or create a jar of the jpa beans from projectA and add the jar as a dependency to projectB.

what ORM use to get access to the database

JDBC is a low level mechanism for interacting with the database, but it's the base of any Java database interaction. The JDBC doesn't offer any transaction management integration, since it only allows you to implement local transactions (based on the current database connection commit/rollback). It's also verbose and on large enterpise applications, with thousand of queries, it's simply pain to add a new column to a base table as you'd have to update zillions of existing queries.

Beside the object to relational mapping ability, ORM tools come with many other useful features:

  1. optimistic locking on complex entity trees
  2. polymorphic queries
  3. local and XA transaction management integration
  4. schema generation
  5. database independent basic querying and DML

But you don't have to use only the ORM tool. In fact this is an anti-pattern on large applications.

You usually mix Hibernate with native querying, or JOOQ to benefit from database specific features (window functions, common table expressions, etc)

The best approach is to use a data layer stack approach, where you pick and mix the best of Hibernate/JDBC/JOOQ.

Perform VACUUM FULL with JPA

As Alay Hay mentioned, using the underlying connection will work:

public void doVacuum(){
org.hibernate.Session session = entityManager.unwrap(org.hibernate.Session);
org.hibernate.internal.SessionImpl sessionImpl = (SessionImpl) session; // required because Session doesn't provide connection()
java.sql.Connection connection = sessionImpl.connection();
connection.prepareStatement("VACUUM FULL").execute();
}


Related Topics



Leave a reply



Submit