Search code examples
jakarta-eejndiglassfish-3

auto registering JNDI resources


I would like to know is it possible to completely register a JNDI resource for a mysql database connection pool just through deploying a war file. Is this possible?

Or is there any other way to work with JNDI files without the admin console. I have a assignment where i have to use JSTL to access the database and we have to deploy this on a central glassfish 3 server on which we dont have access to the admin console. Is this possible?


Solution

  • Yes, there is a way to configure resources without the interaction with the admin console, but this would be server-specific. For example, for glassfish, you would have to create a glassfish-resources.xml file and put it under WEB-INF. Here are the sample contents:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
    
    <resources>
    <jdbc-connection-pool
     name="jdbc/test_pool"
     res-type="javax.sql.DataSource"
     datasource-classname="org.apache.derby.jdbc.ClientDataSource"
     pool-resize-quantity="1"
     max-pool-size="5"
     steady-pool-size="0"
     statement-timeout-in-seconds="30" >
       <property name="PortNumber" value="1527"></property>
          <property name="Password" value="APP"></property>
          <property name="User" value="APP"></property>
          <property name="serverName" value="localhost"></property>
          <property name="DatabaseName" value="testConnection"></property>
          <property name="connectionAttributes" value=";create=true"></property>
     </jdbc-connection-pool>
     <jdbc-resource pool-name="jdbc/test_pool" jndi-name="jdbc/test"></jdbc-resource>
    </resources>
    

    Pay attention, that the created resource will be application-scoped. This means, that you'll have to use java:app prefix to find it by JNDI name, like so:

    @Named
    @SessionScoped
    public class TestConnectionBean implements Serializable {
    
        @Resource(name = "java:app/jdbc/test")
        private javax.sql.DataSource dataSource;
    
        public void test() throws SQLException {
            Connection conn = null;
            try {
                conn = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DatabaseMetaData metaData = conn.getMetaData();
            System.out.println(metaData.getDriverName());
            System.out.println(metaData.getDatabaseProductName());
        }
    
    }
    

    If you need to create a global-scoped resource, then you'll have to use one of the following:

    • admin console
    • CLI asadmin utility
    • edit domain.xml manually