Let me first tell you that I am a rookie when it comes to JPA so I was just following the examples given in EJB3 in Action (albeit with some modification to simplify matters for me). I have installed MySQL and Weblogic 10 (10.3 most probably) and eclipse is the
IDE. Here is the database table in question.
mysql> desc location;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| description | varchar(20) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
Here is the Entity Bean.
package intro.ejb3.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
@Entity
@Table(name="location")
public class Location implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private long id;
private String name;
private String description;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Here is the corresponding persistence.xml.
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="actionBazaar">
<jta-data-source>com.test.onboarding</jta-data-source>
<properties>
<property name="kodo.jdbc.SynchronizeMappings" value="buildSchema"/>
</properties>
</persistence-unit>
</persistence>
And here is the Stateless Session Bean that is getting used as the client.
package intro.ejb3.session.stateless;
import intro.ejb3.entity.Location;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless(mappedName="EventProcessor")
public class EventProcessorBean implements EventProcessor {
@Resource
private SessionContext sessionContext;
public EventProcessorBean() {
}
@PersistenceContext(unitName = "actionBazaar")
private EntityManager em;
public long addLocation(Location location) {
return save(location).getId();
}
@PostConstruct
public void m1() {
}
@PreDestroy
public void m2() {
}
private Location save(Location location) {
System.out.println("Before saving");
em.persist(location);
System.out.println("Location Id : " + location.getId());
return location;
}
}
When I call the addLocation operation I am getting a transaction time-out error from the 'persist' call (the 'Before saving' is getting printed to the server console). Any idea what can be the reason? Please note few things that may be of importance.
1. Initially I was getting some error saying cannot set autocommit true in a distributed transaction (2 phase commit was enable for the datasource). I removed that by selecting 1 phase commit.
2. Setting 1 phase commit resulted into open_jpa_... table related exception which I removed by creating that table manually.
CREATE TABLE openjpa_sequence_table (ID tinyint(4) NOT NULL, SEQUENCE_VALUE bigint(20) default NULL, PRIMARY KEY (ID))
3. Then when I called the operation it was not going to 'Before saving' line even. So I reverted back the configuration to 2 phase commit and then it started giving transaction time out.
4. The Driver class name (as from the Weblogic console) is com.mysql.jdbc.Driver.