Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7928285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:41:35+00:00 2026-06-03T19:41:35+00:00

I have an RMI class that accepts remote calls from clients. This class uses

  • 0

I have an RMI class that accepts remote calls from clients.

This class uses Hibernate to load entities and perform some business logic, in general read-only.

Currently most of the remote methods bodies look like that :

try {
    HibernateUtil.currentSession().beginTransaction();

    //load entities, do some business logic...

} catch (HibernateException e) {
   logger.error("Hibernate problem...", e);
   throw e;
} catch (other exceptions...) {
   logger.error("other problem happened...", e);
   throw e;
} finally {
   HibernateUtil.currentSession().getTransaction().rollback(); //this because it's read-only, we make sure we don't commit anything
   HibernateUtil.currentSession().close();
}

I would like to know if there is some pattern that I could (relatively easily) implement in order to automatically have this “try to open session/catch hibernate exception/finally close hibernate resources” behavior without having to code it in every method.

Something similar to “open session in view” that is used in webapps, but that could be applied to remotr RMI method calls instead of HTTP requests.

Ideally I would like to be able to still call the methods directly, not to use some reflexion passing method names as strings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:41:36+00:00Added an answer on June 3, 2026 at 7:41 pm

    All i wanted was a “quick and clean” solution, if possible, so no new framework for now (I might use Spring+Hibernate stack later on though).

    So I ended up using a “quick-and-not-so-dirty” solution involving a variant of the “Command” pattern, where the hibernate calls are encapsulated inside anonymous inner classes implementing my generic Command interface, and the command executer wraps the call with the Hibernate session and exception handling. The generic bit is in order to have different return value types for the execute method.

    I am not 100% satisfied with this solution since it still implies some boilerplate code wrapped around my business logic (I am especially unhappy about the explicit casting needed for the return value) and it makes it slightly more complicated to understand and debug.

    However the gain in repetitive code is still significant (from about 10 lines to 3-4 lines per method), and more importantly the Hibernate handling logic is concentrated in one class, so it can be changed easily there if needed and it’s less error-prone.

    Here is some of the code :

    The Command interface :

    public interface HibernateCommand<T> {
        public T execute(Object... args) throws Exception; 
    }
    

    The Executer :

    public class HibernateCommandExecuter {
    
        private static final Logger logger = Logger.getLogger(HibernateCommandExecuter.class);
    
        public static Object executeCommand(HibernateCommand<?> command, boolean commit, Object... args) throws RemoteException{
            try {
                HibernateUtil.currentSession().beginTransaction();
    
                return command.execute(args);
    
            } catch (HibernateException e) {
                logger.error("Hibernate problem : ", e);
                throw new RemoteException(e.getMessage());
            }catch(Exception e){
                throw new RemoteException(e.getMessage(), e);
            }
            finally {
                try{
                    if(commit){
                        HibernateUtil.currentSession().getTransaction().commit();
                    }else{
                        HibernateUtil.currentSession().getTransaction().rollback();
                    }
                    HibernateUtil.currentSession().close();
                }catch(HibernateException e){
                    logger.error("Error while trying to clean up Hibernate context :", e);
                }
            }
        }   
    }
    

    Sample use in a remotely called method (but it could be used locally also) :

    @Override
        public AbstractTicketingClientDTO doSomethingRemotely(final Client client) throws RemoteException {
            return (MyDTO) HibernateCommandExecuter.executeCommand(new HibernateCommand<MyDTO>() {
                public AbstractTicketingClientDTO execute(Object...args) throws Exception{
                    MyDTO dto = someService.someBusinessmethod(client);
                    return dto;
                }
            },false);
        }
    

    Note how the client argument is declared final, so it can be referrenced inside the inner class. If not possible to declare final, it could be passed as parameter to the executeCommand method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an RMI server that is using a class from another project. I
I have a simple RMI 'compute' server application (similar to this ) that accepts
Hi I have a class that is a remote object and I implemented methods.
I have a a number of jar files that perform rmi. These are all
I have this class, I m sending this class via RMI and via kryonet
This is a question related to RMI. i have 4 .class files an interface,it's
I have used Naming.rebind(rmi://localhost:1099/RmiServer, c); where c is a remote object. And XYZ robj=new
I have a java RMI app that I want to port to using CORBA.
It's the first time I use java Rmi*. I have a custom class which
I have a doubt regarding RMI. In RMI we create remote object(s) and use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.