Configure Hibernate transactions only with Jersey API Annotations

Asked

Viewed 1,399 times

2

I want to use something similar to org.springframework.transaction.annotation.Transactional Spring that configures a Transaction only using only the Jersey API. Something similar to the code that follows below:

@Resource
private SessionFactory factory;

private Class<E> entity;

private String tableName;

public DataProvider(Class e) {
    this.entity = e;
    this.tableName = entity.getAnnotation(Table.class).name();
}

@Transactional(readOnly = true)
public E get(final Long ID) {
    return (E)factory.getCurrentSession().get(entity, ID);
}

@Transactional(readOnly = true)
public List<E> getAll() {
    Session s = factory.getCurrentSession();
    return s.createQuery("FROM " + tableName ).list();
}

It is possible?

2 answers

4


No, it is not possible to do with Jersey control Hibernate transactions.

Understand that Jersey is made for REST communication and has nothing to do with Hibernate transaction.

You could use CDI to perform transaction control, but it has nothing to do with Jersey.

With CDI you need to create an Interceptor:

@Target({METHOD, TYPE})
@Retention(RetentionPolicy.RUNTIME)
@InterceptorBinding
public @interface Transaction {
    boolean readOnly() default true;
}

And then you would create your developer as:

@Interceptor @Transaction(readOnly = false)
public class MethodWithTransaction {

    @Inject
    private EntityManager entityManager;

    @AroundInvoke
    public Object manageTransaction(InvocationContext context) throws Exception{
        EntityTransaction transaction = null;
        try{
            transaction = entityManager.getTransaction();
            transaction.begin();
            Object methodResult = context.proceed();
            transaction.commit();
            return methodResult;
        } catch (Exception ex){
            if(transaction != null && transaction.isActive()){
                transaction.rollback();
            }

            throw ex;
        }finally {
            entityManager.close();
        }
    }
}

To use you would apply as:

@Transaction(readOnly = false)
public void fazAlgo(){
    //
}

The only difference is that you will need to create an Interceptor for when readonly is true.

  • 1

    Only to complete, since Java EE 7 it is already possible to use the annotation Transactional directly in CDI-administered Beans (i.e., on application servers such as Wildfly and Glassfish 4 simply note the bean, you will not need to implement interceptors). In containers Servlet (Tomcat, Jersey, etc.) you need to configure the implementations at hand (e. g , Weld + Hibernate + Atomikos)

2

If you’re using an application server like Wildfly, you can make your service also an EJB Stateless (add @Stateless at the top of the class), and all REST methods will automatically be involved in a transaction.

If not, why not? By who and looks, you will end up doing a manual integration that is already ready for use on Java EE servers "complete"...

  • There are several reasons not to use a Jboss or Glassfish of life. Not having to engage EJB is already a good reason. To be a java architect, for example, in the proof you have to answer in which EJB cases are not advisable. [=

  • "Not having to engage EJB is already a good reason" -- No, the reason is to know the strengths and weaknesses of EJB. EJB by itself is not disadvantage.

  • Depending on your system, yes EJB will be disadvantage. That’s what I wanted to say. [=

  • 1

    @uaiHebert, unintentionally between flamewars, this is the typical case where EJBS are advisable: need for transactions and a business layer accessing JPA (there is almost no difference in performance between CDI Beans Managed and EJB’s stateless Beans Session). Besides, nowadays I consider the Java EE platform much more practical than Spring. As Bob Dylan would say, The times they are a changin.

  • I think everything goes from need. C you will run only one CRUD project and have limited machine, the Tomcat is a great output. I mean, I always see that it goes from requirement. [=

Browser other questions tagged

You are not signed in. Login or sign up in order to post.