aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Issue with JPA + MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Issue with JPA + MySQL" Watch "Issue with JPA + MySQL" New topic
Author

Issue with JPA + MySQL

Saurav Chatterjee
Greenhorn

Joined: Mar 31, 2005
Posts: 12
This question was under the ORM forum but I didn't get any reply so I thought may be I should re-post it under the EJB3 forum.

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.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Are you sure that your jta-data-source 'com.test.onboarding' is correct? Do you have such datasource configured in your application?

PS. Please use the [ code ] tags to make your code readable - it's really hard to read it as it's a part of the text.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Saurav Chatterjee
Greenhorn

Joined: Mar 31, 2005
Posts: 12

Sorry - reposting this again after using the code tag. Also the datasource is there otherwise it shouldn't have given me the JPA sequence table not found exception.

This question was under the ORM forum but I didn't get any reply so I thought may be I should re-post it under the EJB3 forum.

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.






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.






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.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

So the Location Id:... is never printed? How do you call the EventProcessorBean#save(-)?

PS. Are you required to use Weblogic or is it just your testing-to-get-known-jpa environment?
Saurav Chatterjee
Greenhorn

Joined: Mar 31, 2005
Posts: 12
Here is the client code.




This is a stand alone client with weblogic.jar in the class path. As you can see the addLocation call from the client goes to the session bean (code of which I have pasted earlier) and then from there I call the em.persist
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Ok, and I assume that you're using a KODO as a JPA provider?

It seems that it's some kodo/weblogic specific configuration, as this code should work fine on GF3.1 or JBoss AS7 (at least that's the App servers I've worked with).
Saurav Chatterjee
Greenhorn

Joined: Mar 31, 2005
Posts: 12
Yes - it is Kodo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with JPA + MySQL