The right way to make data sources / resources install installation time

I have a web application that requires two settings:

  • JDBC Data Source
  • Line character

I desperately want to be able to deploy one .war to various containers (berth, tomcat, gf3 minimum) and configure these settings at the application level inside the container.

My code does this:

InitialContext ctx = new InitialContext();
Context envCtx = (javax.naming.Context) ctx.lookup("java:comp/env");
token = (String)envCtx.lookup("token");
ds = (DataSource)envCtx.lookup("jdbc/datasource")

Suppose I used the Glassfish management interface to create two jdbc resources: jdbc / test-datasource and jdbc / live-datasource, which connect to different copies of the same circuit, on different servers, different credentials, etc. Say I want to deploy this to a glass fish and point it to a test data source, I could have this in my sun-web.xml:

...
<resource-ref>
  <res-ref-name>jdbc/datasource</res-ref-name>
  <jndi-name>jdbc/test-datasource</jndi-name>
</resource-ref>
...

but

  • sun-web.xml enters my war, right?
  • ,

? ? , Jetty 7 , .

EDIT Tomcat :

$TOMCAT_HOME/conf/Catalina/localhost/webapp.xml :

<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="false" privileged="true">
  <!-- String resource -->
  <Environment name="token" value="value of token" type="java.lang.String" override="false" />

  <!-- Linking to a global resource -->
  <ResourceLink name="jdbc/datasource1" global="jdbc/test" type="javax.sql.DataSource" />

  <!-- Derby -->
  <Resource name="jdbc/datasource2"
    type="javax.sql.DataSource"
    auth="Container"
    driverClassName="org.apache.derby.jdbc.EmbeddedDataSource"
    url="jdbc:derby:test;create=true"
    />

  <!-- H2 -->
  <Resource name="jdbc/datasource3"
    type="javax.sql.DataSource"
    auth="Container"
    driverClassName="org.h2.jdbcx.JdbcDataSource"
    url="jdbc:h2:~/test"
    username="sa"
    password=""
    />
</Context>

, override="false" . , web.xml.

, , , ; webapp.

, , -, - .

+3
3

GF v3 --deploymentplan deploy asadmin. man .

+2

Tomcat Glassfish 3. .

  • Glassfish ( JDBC) DEV/TEST/PROD/etc.
  • ( ) . :
# Database connection properties
dev=jdbc/dbdev
test=jdbc/dbtest
prod=jdbc/dbprod
  • - .
  • JDBC .

import java.sql.Connection;
import javax.sql.DataSource;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * @param resourceName the resource name of the connection pool (eg jdbc/dbdev)
 * @return Connection a pooled connection from the data source 
 * associated with resourceName
 * @throws NamingException will be thrown if resource name is not found
 */
 public Connection getDatabaseConnection(String resourceName) 
             throws NamingException, SQLException {
    Context initContext = new InitialContext();
    DataSource pooledDataSource = (DataSource) initContext.lookup(resourceName);
    return pooledDataSource.getConnection();
 }

, , "java: comp/env". , , GF3, GF3 web.xml .

+1

, /.

, , , ( ) web.xml.

Application Deployer Administrator (, JNDI , , (, sun-web.xml GlassFish). , , , Java EE.

, , , :

  • - -
  • Reconfigure an existing data source to point to another database.

Having an admin interface does not change anything. If I missed something, feel free to let me know. And just in case, maybe look at this previous answer .

0
source

Source: https://habr.com/ru/post/1734596/


All Articles