You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Kalyan <ka...@gmail.com> on 2019/12/10 18:02:16 UTC
Datasource configuration issue while configuring same database with
two different datasource
Hello
We are trying to use OpenEjb in Embedded Mode (Inside Netty based server) by
following the below specified document (please see Code snippet at the end).
http://tomee.apache.org/datasource-config.html (Section: Alternatively, a
DataSource can be declared via properties in the
<tomee-home>/conf/system.properties....... )
The application needs to connect to multiple data sources (different JNDI
Data source names but same Oracle DB) and for performance reasons we are
setting the "InitialSize" and "MaxTotal" to same value (15) for both data
sources.
After setting up the context and starting the server, when we check how many
DB Connections are pre-established (using netstat command) we only see that
Connections are pre-created for 1st DataSource, for the other datasource
there are no connections is pre-created.
Runtime env Adopt JDK 12
Can you please help me in figuring if my configs are wrong?
netstat command: netstat -anp | grep <<Java Process ID>> | grep 1521 (Oracle
Port Number for both data sources)
Java Process ID: 27010
NetStat Output:
tcp6 0 0 <<Host IP>>:53571 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53565 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53574 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53561 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53575 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53569 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53566 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53570 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53564 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53572 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53567 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53573 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53560 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53562 <<DB IP>>:1521 ESTABLISHED
27010/java
tcp6 0 0 <<Host IP>>:53568 <<DB IP>>:1521 ESTABLISHED
27010/java
Code:
import org.apache.log4j.Logger;
import java.util.*;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
public class OpenEjbContext implements Context {
final static Logger logger = Logger.getLogger("DSLog");
private InitialContext initialContext;
private Properties jndiMappingProperties;
public OpenEjbContext( ) throws NamingException {
logger.error("OpenEjbContext.. start ");
Properties contextProperties = new Properties();
setOpenEjbContextProperties(contextProperties);
setOpenEjbDataSourcesProperties(contextProperties);
initialContext = new InitialContext(contextProperties);
logger.error("OpenEjbContext created successfully.. end ");
}
/**
* sets the openejb specific properties.
* @param contextProperties
*/
private void setOpenEjbContextProperties(Properties contextProperties) {
//OpenEJB - EJB Pool Specific Configs:
contextProperties.put("java.naming.factory.initial","org.apache.openejb.core.LocalInitialContextFactory");
contextProperties.put("openejb.deployments.classpath.ear","false");
contextProperties.put("myAppl","new://Container?type=STATELESS");
contextProperties.put("myAppl.AccessTimeout","5 seconds");
contextProperties.put("myAppl.MaxSize","10");
contextProperties.put("myAppl.MinSize","0");
contextProperties.put("myAppl.StrictPooling","true");
contextProperties.put("myAppl.MaxAge","0 hours");
contextProperties.put("myAppl.ReplaceAged","true");
contextProperties.put("myAppl.ReplaceFlushed","false");
contextProperties.put("myAppl.MaxAgeOffset","-1");
contextProperties.put("myAppl.IdleTimeout", "0 minutes");
contextProperties.put("myAppl.SweepInterval", "5 minutes");
contextProperties.put("myAppl.CallbackThreads","5");
contextProperties.put("myAppl.CloseTimeout","5 minutes");
contextProperties.put("myAppl.UseOneSchedulerThreadByBean", "false");
contextProperties.put("myAppl.EvictionThreads", "1");
//OpenEJB related configs -- logging related
contextProperties.put("openejb.logger.external","true");
contextProperties.put("log4j.rootLogger", "fatal,C");
contextProperties.put("log4j.category.OpenEJB", "warn");
contextProperties.put("log4j.category.OpenEJB.options", "debug");
contextProperties.put("log4j.category.OpenEJB.server", "debug");
contextProperties.put("log4j.category.OpenEJB.startup", "debug");
contextProperties.put("log4j.category.openejb.jdbc.log", "true");
contextProperties.put("log4j.appender.C",
"org.apache.log4j.ConsoleAppender");
contextProperties.put("log4j.appender.C.layout",
"org.apache.log4j.SimpleLayout");
}
private void setOpenEjbDataSourcesProperties(Properties
contextProperties) {
//OpenEJB DB Sources related configs
contextProperties.put("openejb.datasource.pool", "true");
contextProperties.put("openejb.jdbc.datasource-creator","dbcp");
//TRIED using dbcp-alternate but same behavior
//-------- DataSource1 Configs --------
contextProperties.put("DataSource1",
"new://Resource?type=javax.sql.DataSource");
contextProperties.put("DataSource1.JdbcDriver",
"oracle.jdbc.OracleDriver");
contextProperties.put("DataSource1.JdbcUrl",
"jdbc:oracle:thin:@dev-lapp209.com:1521/dsDB");
contextProperties.put("DataSource1.UserName", "USER1");
contextProperties.put("DataSource1.Password", "password");
contextProperties.put("DataSource1.LogSql", "false");
contextProperties.put("DataSource1.JtaManaged", "true");
contextProperties.put("DataSource1.initialSize", 15);
//"MaxTotal" should be used as opposed to "MaxActive" when using
dbcp.
contextProperties.put("DataSource1.MaxTotal", 15);
contextProperties.put("DataSource1.minIdle", 15);
contextProperties.put("DataSource1.maxIdle", 15);
contextProperties.put("DataSource1.connectionProperties",
"[oracle.jdbc.ReadTimeout=55000;oracle.net.CONNECT_TIMEOUT=30;]");
//-------- DataSource2 Configs --------
contextProperties.put("DataSource2",
"new://Resource?type=javax.sql.DataSource");
contextProperties.put("DataSource2.JdbcDriver",
"oracle.jdbc.OracleDriver");
contextProperties.put("DataSource2.JdbcUrl",
"jdbc:oracle:thin:@dev-lapp209.com:1521/dsDB");
contextProperties.put("DataSource2.UserName", "USER1");
contextProperties.put("DataSource2.Password", "password");
contextProperties.put("DataSource2.LogSql", "false");
contextProperties.put("DataSource2.JtaManaged", "true");
contextProperties.put("DataSource2.initialSize", 15);
//"MaxTotal" should be used as opposed to "MaxActive" when using
dbcp.
contextProperties.put("DataSource2.MaxTotal", 15);
contextProperties.put("DataSource2.minIdle", 15);
contextProperties.put("DataSource2.maxIdle", 15);
contextProperties.put("DataSource2.connectionProperties",
"[oracle.jdbc.ReadTimeout=55000;oracle.net.CONNECT_TIMEOUT=30;]");
}
/**
* openejb creates jndi in the different namespace than other
containers.
* So to support the jboss jndi. We need to map with the openejb format
of jndi.
*/
@Override
public Object lookup(String name) throws NamingException {
Map<String, String> old2NewMapping = new LinkedHashMap<>();
//data source mappings
old2NewMapping.put("java\\:/DataSource1",
"java:openejb:Resource/DataSource1");
old2NewMapping.put("java\\:/DataSource2",
"java:openejb:Resource/DataSource2");
//ejb mappings
old2NewMapping.put("ds/BaseServiceSB", "BaseServiceBeanLocalHome");
String jndiMappingName = old2NewMapping.get(name);
if(jndiMappingName == null) {
throw new RuntimeException("Jndi mapping is missing for " +
jndiMappingName);
}
return initialContext.lookup(jndiMappingName);
}
@Override
public Object lookup(Name name) throws NamingException {
return initialContext.lookup(name);
}
@Override
public void bind(Name name, Object obj) throws NamingException {
initialContext.bind(name, obj);
}
@Override
public void bind(String name, Object obj) throws NamingException {
initialContext.bind(name, obj);
}
@Override
public void rebind(Name name, Object obj) throws NamingException {
initialContext.rebind(name, obj);
}
@Override
public void rebind(String name, Object obj) throws NamingException {
initialContext.rebind(name, obj);
}
@Override
public void unbind(Name name) throws NamingException {
initialContext.unbind(name);
}
@Override
public void unbind(String name) throws NamingException {
initialContext.unbind(name);
}
@Override
public void rename(Name oldName, Name newName) throws NamingException {
initialContext.rename(oldName, newName);
}
@Override
public void rename(String oldName, String newName) throws NamingException
{
initialContext.rename(oldName, newName);
}
@Override
public NamingEnumeration<NameClassPair> list(Name name) throws
NamingException {
return initialContext.list(name);
}
@Override
public NamingEnumeration<NameClassPair> list(String name) throws
NamingException {
return initialContext.list(name);
}
@Override
public NamingEnumeration<Binding> listBindings(Name name) throws
NamingException {
return initialContext.listBindings(name);
}
@Override
public NamingEnumeration<Binding> listBindings(String name) throws
NamingException {
return initialContext.listBindings(name);
}
@Override
public void destroySubcontext(Name name) throws NamingException {
initialContext.destroySubcontext(name);
}
@Override
public void destroySubcontext(String name) throws NamingException {
initialContext.destroySubcontext(name);
}
@Override
public Context createSubcontext(Name name) throws NamingException {
return initialContext.createSubcontext(name);
}
@Override
public Context createSubcontext(String name) throws NamingException {
return initialContext.createSubcontext(name);
}
@Override
public Object lookupLink(Name name) throws NamingException {
return initialContext.lookupLink(name);
}
@Override
public Object lookupLink(String name) throws NamingException {
return initialContext.lookupLink(name);
}
@Override
public NameParser getNameParser(Name name) throws NamingException {
return initialContext.getNameParser(name);
}
@Override
public NameParser getNameParser(String name) throws NamingException {
return initialContext.getNameParser(name);
}
@Override
public Name composeName(Name name, Name prefix) throws NamingException {
return initialContext.composeName(name, prefix);
}
@Override
public String composeName(String name, String prefix) throws
NamingException {
return initialContext.composeName(name, prefix);
}
@Override
public Object addToEnvironment(String propName, Object propVal) throws
NamingException {
return initialContext.addToEnvironment(propName, propVal);
}
@Override
public Object removeFromEnvironment(String propName) throws
NamingException {
return initialContext.removeFromEnvironment(propName);
}
@Override
public Hashtable<?, ?> getEnvironment() throws NamingException {
return initialContext.getEnvironment();
}
@Override
public void close() throws NamingException {
/* This left blank. Since the context is getting cleared and all the
properties
* set before are getting nullified.*/
}
@Override
public String getNameInNamespace() throws NamingException {
return initialContext.getNameInNamespace();
}
}
--
Sent from: http://tomee-openejb.979440.n4.nabble.com/TomEE-Users-f979441.html