Proper usage of JDBC Connection Pool (Glassfish)

后端 未结 3 834
攒了一身酷
攒了一身酷 2020-12-01 15:02

I need a database connection in Java Web service implemented as a session bean, and I\'m not sure if I do it right.

I created a class

public final          


        
相关标签:
3条回答
  • 2020-12-01 15:34

    Apart from the C-style formatting, a few unnecessary lines and a bit poor exception handling, you can just do so.

    Here's how I'd do it:

    public final class SQLUtil {
        private static DataSource dataSource;
        // ..
    
        static {
            try {
                dataSource = (DataSource) new InitialContext().lookup(name);
            } catch (NamingException e) {
                throw new ExceptionInInitializerError(e);
            }
        }
    
        public static Connection getConnection() throws SQLException {  
            return dataSource.getConnection();             
        }
    }
    

    I throw here ExceptionInInitializerError so that the application will immediately stop so that you don't need to face "unexplainable" NullPointerException when trying to obtain a connection.

    0 讨论(0)
  • 2020-12-01 15:36

    In the ancient J2EE world, the traditional way to manage this was to use a ServiceLocator. Below, a sample implementation (non optimized, the DataSource could be cached):

    public class ServiceLocator {
        private Context initalContext;
    
        private static ServiceLocator ourInstance = new ServiceLocator();
    
        public static ServiceLocator getInstance() {
            return ourInstance;
        }
    
        private ServiceLocator() {
            try {
                this.initalContext = new InitialContext();
            } catch (NamingException ex) {
                throw new ServiceLocatorException(...);
            }
        }
    
        public DataSource getDataSource(String dataSourceName) {
            DataSource datasource = null;
    
            try {
                Context ctx = (Context) initalContext.lookup("java:comp/env");
                datasource = (DataSource) ctx.lookup(dataSourceName);
            } catch (NamingException ex) {
                throw new ServiceLocatorException(...);
            }
    
            return datasource;
        }
    }
    

    To use it, simply call it like this:

    DataSource ds = ServiceLocator.getInstance().getDataSource("jdbc/mydatabase");
    

    But this was prior to the EJB3 and Dependency Injection era. Now, when using EJB3, if you have setup your DataSource in your EJB container, all you have to do to automatically inject the DataSource in your Stateless Bean is to write (where mydatabase is the name of the datasource):

    @Resource
    private DataSource mydatabase;
    

    Use the name attribute if you want to explicitly, well, set the name:

    @Resource(name="jdbc/mydatabase")
    private DataSource dataSource;
    

    EJB3 actually make the ServiceLocator pattern obsolete and you should really prefer injection when working with them.

    0 讨论(0)
  • 2020-12-01 15:39

    Um, isn't this an example to a JDBC DataSource, not a Glassfish Connection Pool?

    0 讨论(0)
提交回复
热议问题