wiki:KrakenJpa

Kraken JPA

Kraken JPA is based on hibernate 3 and provides declarative transaction feature.

Requirements

  • kraken api 1.7.0
  • slf4j api 1.5.6
  • ipojo 1.4.0
  • hibernate 3.3.0.SP1
  • hibernate annotations 3.4.0.GA
  • hibernate entity manager 3.4.0.GA
  • hibernate commons annotations 3.3.0.GA
  • hibernate c3p0 3.3.2.GA
  • c3p0 0.9.1.2

Install

kraken> pkg.install kraken-jpa

Install JDBC Driver

kraken> jpa.installDriver
Database
-------------
[1] MySQL
[2] PostgreSQL
select? 1
Resolving com.mysql.jdbc/com.springsource.com.mysql.jdbc (5.1.6)
  -> trying to download from http://download.krakenapps.org/
bundle [18] loaded

POM Configuration

<Import-Package>
  javassist.util.proxy,
  org.hibernate.proxy,
  org.hibernate.exception,
  org.hibernate,
  org.hibernate.dialect,
  javax.persistence,*
</Import-Package>
  • You MUST import above packages for JPA model bundle.
    • Otherwise, you will encounter HibernateProxy ClassNotFoundException.

iPOJO Handler Configuration

@JpaConfig(factory = "entityManagerFactoryName")
@Component(name = "example-component")
public class ExampleComponent {
    @Requires
    private ThreadLocalEntityManagerService entityManagerService;
}
  • ThreadLocalEntityManagerService.getEntityManager() will return an EntityManager object in current thread context.
  • entityManagerFactoryName is alias for JPA entity manager factory.

Database Configuration

  • Kraken JPA recognizes two configuration files in bundle.
    • OSGI-INF/kraken-jpa/classes file have JPA entity classe names line by line.
    • OSGI-INF/kraken-jpa/config file contains hibernate configurations. (properties file format)
  • You can load JPA entity bundle at kraken console.
    • "jpa.register [bundle id] [entity manager factory name]" script command will load above configuration files in specified bundle and create entity manager factory with configurations.
  • You can also create entity manager factory programmatically.
    • Find the service reference for org.krakenapps.jpa.JpaService interface.
      • e.g. BundleContext.getServiceReference(JpaService.class.getName());

Configure C3P0 Properties

connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider
hibernate.c3p0.min_size = 1
hibernate.c3p0.max_size = 100
hibernate.c3p0.timeout = 0
hibernate.c3p0.max_statements = 0
  • connection.provider_class
    • For use C3P0, provider class must set org.hibernate.connection.C3P0ConnectionProvider.
  • hibernate.c3p0.min_size
    • Minimum number of JDBC connections in the pool. (default: 1)
  • hibernate.c3p0.max_size
    • Maximum number of JDBC connections in the pool. (default: 100)
  • hibernate.c3p0.timeout
    • When an idle connection is removed from the pool (in second). (default: 0 (never expire))
  • hibernate.c3p0.max_statements
    • Number of prepared statements will be cached. Increase performance. (default: 0)

Declarative Transaction Support

  • @Transactional method annotation provides declarative transaction feature.
    • ThreadLocalEntityManagerService will begin transaction at method entry and commit transaction at method exit. If any error occured, transaction will be rollbacked. Finally, the EntityManager object closed.
  • TransactionOption (from 1.1.0 release)
    • @Transactional has TransactionOption.Required by default.
    • TransactionOption.Required: Shares a transaction, if one exists, and creates a new transaction if necessary.
    • TransactionOption.RequiresNew: Executes the method with a new transaction, regardless of the state of the current context.
    • TransactionOption.Supported: Shares a transaction, if one exists.

Kraken JPA Script

  • jpa.list: list all registered entity manager factories.
  • jpa.register [bundle id] [entity manager factory name]: load configuration from specified bundle and register entity manager factory.
  • jpa.unregister [entity manager factory name]: unregister and close entity manager factory.

Kraken JPA Class Diagram

History

  • 1.5.0 release
    • Added C3P0 Connection Provider.
  • 1.4.0 release
    • Upgraded to JPA 2.0 and Hibernate 3.5.4 Final.
    • Added connection validation for JPA model registration process.
  • 1.1.1 release: Fixed handler validation bug at boot time.
  • 1.1.0 release: Added TransactionOption to @Transactional. Nested @Transactional declaration is supported.
  • 1.0.0 release: Implemented using hibernate.