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

  • Home
  • SEARCH
  • 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 6143669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:33:06+00:00 2026-05-23T18:33:06+00:00

I am new to the Java EE World and want to use PostgreSQL as

  • 0

I am new to the Java EE World and want to use PostgreSQL as my database in an web application. I use Glassfish as my application server and added a connection pool via the administration interface (I used this site for help). Now I don’t know what is the “right” way to use the connection pool in my application (in fact I currently don’t know how to even get an connection from the pool and write a simple query).

We need to write pretty complex queries, so I don’t know if I should create a mapping for every table and use the mappings, or just use sql and some kind of row mapper to pars the results (we used the Spring RowMapper before).

So my Question are:

  1. What different ways are there to use the connections from the pool?
  2. What are the (dis)advantages of these patterns?
  3. How to create a clever mapping that can handle complicated and performance intensive query’s.
  4. Does this work with Hibernate? And if so, how?
  • 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-05-23T18:33:06+00:00Added an answer on May 23, 2026 at 6:33 pm

    1 – If your connection pool is configured using glassfish, you may develop a simple EJB and inject the connection using annotation, I think this is the best way to handle connexion from your pool.

    (All examples are compatible with hibernate and work well on postgresql database)

    eg:

    @Stateless
    public class myEjb
    {
        // inject the entityManager  
        @PersistenceContext(unitName = "myPu")
        private EntityManager em;
    
        public Car findCarById(Long carId)
        {
            Car aCar = (Car) em.find(Car.class, carId);
            return aCar;
        }
    }
    

    the unitName “myPu” is the JNDI name of your JDBC resource configured in the glassfish administration console.

    2 – the advantage of this pattern is that it is not code dependant, you may not change your JDBC ressource regarding the dev, test or production environment. The JDBC url, user and pass are configured on the server and not in an XML file that you may carefully change each time you switch from an environment to another. This way you don’t have to handle the transaction, this is the server that commit or rollback on exception.
    If you need to handle the transaction yourself, you may add the annotation under the Stateless one:

    @TransactionManagement(value=TransactionManagementType.BEAN) 
    

    you the need to handle by yourself the transaction like:

    em.getTransaction().begin();
    try
    {
        // do something with the entityManager
        em.getTransaction().commit();
    }
    catch(MyException ex)
    {
        em.getTransaction().rollback();
    }
    

    3 – If you need to create complexe request, you may declare some named queries on your entity.
    below a possible implementation of the Car entity:

    @Entity
    public class Car 
    {
        @NamedQueries(
        {
            @NamedQuery(name = "Car.findByBrandAndColor", query = "SELECT c FROM Car WHERE c.brand = :brand AND color = :color")
        })
    }
    

    below an example of function to add to your EJB that use the previous named query:

    public List<Car> findAllCarByBrandAndColor(String aBrand, String aColor)
    {
        List<Car> theCars;
    
        Query theQuery = em.createNamedQuery("Car.findByBrandAndColor");
        theQuery.setParameter("brand", aBrand);
        theQuery.setParameter("color", aColor);
    
        try
        {
            theCars = (List<Car>) query.getResultList();
        }
        catch(NoResultException ex)
        {
            theCars = null;
        }
    
        return theCars;
    }
    

    Of course, you can write named queries as complex as you want (named queries are cached and may be better for performance), but if you really need to query directly the database, you may use native queries as below:

    // the query is native SQL
    Query theQuery = em.createNativeQuery("SELECT * FROM car WHERE color = 'blue' AND brand = 'toyota'", Car.class);
    Car aCar = (Car) query.getSingleResult();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use HttpURLConnection to open a long-living connection to a server via
I am a newbie to java and android world, and I want to use
I'm trying to build a new web app using Eclipse, I want to use
I am new to Java world but I am pretty good at using Flex,
I'm new in the Java world and I'm totally confused with the sheer number
I'm brand new to java, coming from a ruby world. One thing I love
I am pretty new to the whole Java and OSGi world and I have
I'm totally new to both java and java Server worlds... But I've a good
For a new Java web project I thought about using: Eclipse as IDE Mercurial
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring

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.