Skip to content

Using Kundera with Jboss

Devender Yadav edited this page Mar 24, 2015 · 3 revisions

Jboss is an application server that supports container managed entities for some ORM tools, but it doesn't provide container managed entity support for Kundera, so for using Kundera with it you need to manage the persistence context at application level called application managed persistence context.

Before we get into detail on how to configure Kundera with Jboss let's understand persistence context and application managed persistence context.

Entities are managed by the entity manager. Each entiity manager is associated with a persistence context. Persistence context includes a set of managed entity instances.

When using container managed entity manager, entity manager's persistence context is managed by the container and is propagated to all the application components using that uses it. To obtain an EntityManager instance, inject it into the application component:

@PersistenceContext
EntityManager em;

Instead of depending on a container to provide persistence context for the application we can manage the persistence context on our own at the application level,which is known as Application Managed Persistence Context.In this case the lifecycle of entity manger is managed by the application where applications create EntityManager instances by using the createEntityManager method of javax.persistence.EntityManagerFactory.

To obtain an EntityManager instance, you must first obtain an EntityManagerFactory instance by injecting it into the application component by using the javax.persistence.PersistenceUnit annotation:

@PersistenceUnit
EntityManagerFactory emf;
Then obtain an EntityManager from the EntityManagerFactory instance:
EntityManager em = emf.createEntityManager();

Application-managed entity managers don’t automatically propagate the JTA transaction context. Such applications need to manually gain access to the JTA transaction manager and add transaction demarcation information when performing entity operations. The javax.transaction.UserTransaction interface defines methods to begin, commit, and roll back transactions. Inject an instance of UserTransaction by creating an instance variable annotated with @Resource:

@Resource
UserTransaction utx;

To begin a transaction, call the UserTransaction.begin method. When all the entity operations are complete, call the UserTransaction.commit method to commit the transaction. The UserTransaction.rollback method is used to roll back the current transaction.

The code below shows an example of application managed persistence context:

@PersistenceUnit
EntityManagerFactory emf;
EntityManager em;
@Resource
UserTransaction utx;
...
em = emf.createEntityManager();
try {
  utx.begin();
 em.persist(SomeEntity);
  em.merge(AnotherEntity);
 em.remove(ThirdEntity);
  utx.commit();
} catch (Exception e) {
  utx.rollback();
}

This is how application managed entities work. But you can also use some frameworks to manage your persistence context, you can delegate the responsibility managing your entities to a framework like spring.

First of all you have to tell that JBoss is not managing the entities, by using the following property in in the persistence.xml

<property name="jboss.as.jpa.managed" value="false" />

which states that the entities are to application managed and not to be managed by jboss.

Spring mvc can manage your entities like a jboss container does. For that when you create entity manager, you need to use the following syntax

 @PersistenceContext(unitName="cassandra-pu,mongo-pu", type=PersistenceContextType.EXTENDED)
 private EntityManager em;

When the persistence context is managed by spring then you need to include some properties in application-context.xml. Following are the properties that needs to be part of your application.

<bean id="emf-p"
	`class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="cassandra-pu" />
	<property name="loadTimeWeaver">
	<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
	</property>
</bean>

When you have single persistence unit in your persistence.xml then just use the above property with the name of your persistence unit. Here its “cassandra-pu”.

In case of polyglot persistence you need to define two persistence units, like shown below.

<!-- emf-p1 and emf-p2 are required only in case of polyglot persistence
	(i.e. using multiple cross data store persistence unit -->
<bean id="emf-p1"
	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="cassandra-pu" />
	
<property name="loadTimeWeaver">
		
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
	</property>
</bean>
<bean id="emf-p2"
	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="mongo-pu" />
	<property name="loadTimeWeaver">
	<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
	</property>
</bean>

Finally you need to give the location of your persistence.xml like shown below.

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
</bean>
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>META-INF/persistence.xml</value>
</list>
</property>
</bean>

Setup the web based project using Kundera getting started.

https://github.com/impetus-opensource/Kundera/wiki/Getting-Started-in-5-minutes

After creating the project build it and put it in jboss/standalone/deployments and run jboss/bin/standalone.These setup steps are described for jboss-as-7.1.1.Final but same should also work for other versions of JBoss as well.

A sample project for same is available at

https://github.com/impetus-opensource/Kundera/tree/trunk/examples

It is a jsf project which uses Kundera as its object data mapper and spring to manage its persistence context and uses cassandra and mongodb for persisting data. Below are some snapshots from the application which you should be able to view once your application is deployed successfully.

  1. Home Page

Home

2.Registration

registration

3.User Profile

Profile

4.Upload Files

upload file

5.See Uploaded Files

uploaded files

6.Log Out

signed out

Clone this wiki locally