• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jboss7, DAO, JPA, Problem getting transaction (javax.persistence.TransactionRequiredException)

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

hopefully some of you can help me: I want to deploy an application into JBoss7. This app should use Container-managed persistency (JPA). Now the problem is that I can't update the database, because i'm not able to get a transaction.

This is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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="myDS" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>

<classes>....deleted....</classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="jboss.entity.manager.jndi.name" value="myDSEntityManager"/>
<property name="jboss.entity.manager.factory.jndi.name" value="SchemaDSEntityManagerFactory" />
<!-- property name="transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory"/-->
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>

The Entity-classes are defined like this:

@Entity
@NamedQuery(name="Pricedata.findAll", query="SELECT p FROM Pricedata p")
public class Pricedata implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private Integer id;

@Column(name="some_id")
private String someId;
...
}

Now the DAO-classes:

public class PricedataDao implements DaoInterface<Pricedata, String> {

private EntityManager em;
final Logger logger = Logger.getLogger(getClass());
private Context ic = null;

public void init()
{
try {

if (ic == null)
ic = new InitialContext();
if (em == null || !em.isOpen())
em = ((EntityManagerFactoryImpl)ic.lookupLink("java:/SchemaDSEntityManagerFactory")).createEntityManager();
if (em==null) {
logger.error("EntityManager not set.");
}
} catch (Exception e) {
logger.error("Error on getting emf: " + e);
e.printStackTrace();
}
}

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public ArrayList<Integer> updateByDto(ArrayList<dto.myPrice> pspList) {
init();

if (em==null) {
logger.error("updateByDto(): not possible, EntityManager not instantiated.");
return null;
}

ArrayList<Integer> al = null;
String nativeSql = "";
try {
for (int i=0; i<pspList.size(); i++) {
dto.myPrice psp = pspList.get(i);
nativeSql = "insert into schema.Pricedata (id, ....";

Query q = em.createNativeQuery(nativeSql);

int retCode = q.executeUpdate();
....

The error-message I get while executing q.executeUpdate is an javax.persistence.TransactionRequiredException.
After a few hours of trying i'm now blind to find the problem.
The days before we migrated the whole project from application-managed persistency (getting EntityManagerFactory via Persistence.createEntityManagerFactory()) to CMT.

Hopefully I made a small problem only and you can point me in the right direction.

Best regards, Carsten
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic