2

Please help me to understand why object is not getting persisted with following code. It throws javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context)

public class OrganizationRepositoryImpl implements OrganizationRepository {

    @PersistenceContext(unitName="usermanagement",type=PersistenceContextType.TRANSACTION)
    private EntityManager em;

    public void save(Organization organization) {
        try {
            em.persist(organization);
        }catch(Exception e) {
            e.printStackTrace();

        }
    }    
}

But If I annotate the class with @Stateless annotation(Now ejb), object start getting persisted

@Stateless    
public class OrganizationRepositoryImpl implements OrganizationRepository {

            @PersistenceContext(unitName="usermanagement",type=PersistenceContextType.TRANSACTION)
            private EntityManager em;

            public void save(Organization organization) {
                try {
                    em.persist(organization);
                }catch(Exception e) {
                    e.printStackTrace();

                }
            }    
        }

persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
        version="2.1">
    <persistence-unit name="usermanagement" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/MysqlXADS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
             <property name="hibernate.show_sql" value="true"/>
             <property name="hibernate.format_sql" value="true"/>  
             <property name="use_sql_comments" value="true"/>
             <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/myEntityManagerFactory" /> 
        </properties>
    </persistence-unit>
</persistence>

Service class to call Repository to persist the object to the database

    public class OrganizationServiceImpl implements OrganizationService {

    @Inject
    private OrganizationRepository orgRepo;

    public Response createOrganization(InputStream is) {
        Organization org = null;
        org = readStream(is);
        orgRepo.save(org);
        return Response.created(URI.create("/organizations/" + org.getId()))
                .build();
    }

    private Organization readStream(InputStream is) {
        JAXBContext context;
        Organization org = null;
        try {
            context = JAXBContext.newInstance(Organization.class);
            Unmarshaller um = context.createUnmarshaller();
            org = (Organization) um.unmarshal(is);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return org;
    }
}  
VirtualLogic
  • 706
  • 1
  • 10
  • 25

2 Answers2

9

When you declare the bean as @Stateless then the methods in that bean are by default transactional. Transactional methods commit the persistence state when fully executed.

When you don't have your class annotated with @Stateless methods are not transactional by default and hence you get the mentioned exception.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • That's true, so how do I make it methods of a bean Transactional without making the bean as EJB(@Stateless). I tried annotating method with @Transactional(vale=TxType=Required) but still getting same exception. – VirtualLogic Apr 25 '14 at 18:25
  • 1
    Another day, another annotation that has a other behaviour that isn't clearly documented. – Spence Feb 16 '16 at 22:23
4

I'm not too familiar with EJBs, but I believe the problem is that without the @Stateless annotation, the bean is not a EJB, so transactions are not automatically managed. If you need statefulness, use the @Stateful annotation to make a stateful EJB. If you do not want to use EJBs, you have to manually manage your transactions using the EntityManager like so.

tx = em.getTransaction();
tx.begin();

// do some work
...

tx.commit();
Brian Blonski
  • 1,762
  • 18
  • 23