This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Issue with JPA + MySQL

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic