Friday, July 18, 2014

JPA 2.1 Performance Tuning Tips

Here is some performance tuning tips while dealing with JPA 2.1.




To get more details about Java EE 7 performance tuning, check my book at the following URL: http://www.packtpub.com/java-ee-7-performance-tuning-and- optimization/book

Wednesday, July 2, 2014

Win "Java EE 7 Performance Tuning and Optimization" Book - CLOSED



Book Give-away:

Hold a chance to win free copy of the Java EE 7 Performance Tuning and Optimization, just by commenting about the book with the link - http://www.packtpub.com/java-ee-7-performance-tuning-and-optimization/book!

For the contest we have 5 e-copies each of the book Java EE 7 Performance Tuning and Optimization, to be given away to 5 lucky winners.

How you can win:

To win your copy of this book, all you need to do is come up with a comment below:
1) Highlighting the reason "why you would like to win this book” 
2) Add the link of the book - http://www.packtpub.com/java-ee-7-performance-tuning-and-optimization/book
3) Adding a book tweet link or social media post in the comment will increase the chance to win the book.

Note – To win, you must follow the previous 3 steps.

Duration of the contest & selection of winners:

The contest is valid for 1 week, and is open to everyone. Winners will be selected on the basis of their comment posted.
We will announce the winners in this post and ask them to provide their contact email addresses.




NOW the winners are listed below in the comments, please send me your emails, good luck for others.

Tuesday, July 1, 2014

Using JPA with Google App Engine Object Store

We can use JPA while dealing with Object Store in Google App Engine, this abstracts the interaction with object store.
Because both Object store and JPA are object-based there is good matching between both technologies.
Here is the simple steps we need to follow:

1) Create the JPA beans
- Including annotation
- Define primary key
- Setters/Getters for fields.
Here is an example of using Locate object as JPA bean:
@PersistenceCapable(identityType=IdentityType.DATASTORE)
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Long id;
@Persistent
public int entityId;
@Persistent
public double lat;
@Persistent
public double lon;
@Persistent
public double accuracy;
@Persistent
public long time;

public Location(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
...
... //getters and setters
}

We used auto-generation primary key strategy here.

2) Create session facade or abstraction layer or DAO object to interact with this bean

public class LocationDAO {
}

3) Define PersistenceManagerFactory object

private static final PersistenceManagerFactory persistenceFactory = JDOHelper
.getPersistenceManagerFactory("transactions-optional");


4) Get PersistenceManager object to use for JPA interactions

PersistenceManager pm = pmfInstance.getPersistenceManager();


5) Example for ADD/CREATE operation

public Location addLocation(int entityId, double lat, double lon, double accuracy,long time){
Location location = null;
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
location=new Location();
location.setEntityId(entityId);
location.setLat(lat);
location.setLon(lon);
location.setAccuracy(accuracy);
location.setTime(time);
pm.makePersistent(location);
} finally {
pm.close();
}
return location;
}

6) Example for DELETE operation

public void removeLocation(Location location) {
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
pm.currentTransaction().begin();
location = pm.getObjectById(Location.class, location.getId());
pm.deletePersistent(location);
pm.currentTransaction().commit();
} catch (Exception ex) {
pm.currentTransaction().rollback();
throw new RuntimeException(ex);
} finally {
pm.close();
}
}

7) Example of UPDATE operation

public void updateLocation(Location location,int entityId, double lat, double lon, double accuracy,long time) {
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
pm.currentTransaction().begin();
location = pm.getObjectById(Location.class, location.getId());
location.setEntityId(entityId);
location.setLat(lat);
location.setLon(lon);
location.setAccuracy(accuracy);
location.setTime(time);
pm.makePersistent(location);
pm.currentTransaction().commit();
} catch (Exception ex) {
pm.currentTransaction().rollback();
throw new RuntimeException(ex);
} finally {
pm.close();
}
}

8) Example of SELECT operation

public List getAllLocationsForEntity(int entityId){
PersistenceManager pm = persistenceFactory.getPersistenceManager();
String queryStr = "select from " + Location.class.getName();
Query selectQuery=pm.newQuery(queryStr);
selectQuery.setFilter("entityId=="+entityId);
return (List) selectQuery.execute();
}

These are the simple basic CRUD operations to perform on Object data store in Google App Engine using JPA.