I have wrote Java Client application (bean for Oracle form) to access the data through "jdbcdatasource" created on Weblogic 12c. It works fine when I am running on desktop, but when I am embedding on oracle forms as a bean, it gives following error:
java.lang.ClassNotFoundException: weblogic.jdbc.common.internal.RmiDataSource_12210_WLStub
java bean is an executable jar file includes all the dependency jar file, and it is executed independently by double click.
Here is a code:
Context ctx = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3:" + url + ":7001");
if(sUser != null && sPwd != null){
ht.put(Context.SECURITY_PRINCIPAL, sUser);
ht.put(Context.SECURITY_CREDENTIALS, sPwd);
}
ctx = new InitialContext(ht);
System.out.println("!!! WebLogic Server Connection Done!");
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("myDatasource");
java.sql.Connection conn = ds.getConnection();
System.out.println("!!! DataSource Connection Done!");
In the environment of Oracle forms it connect to the weblogic server but could not access the data source by displaying above error.
Any suggestion?
Just to make things clear to me.
When you say :
"to access the data through "jdbcdatasource" created on Weblogic 12c."
and you code shows:
"javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("myDatasource");"
The lookup parameter value shouldn't be exactly like "jdbcdatasource" instead of "myDataSource' as you stated before or it was just to explain your situation ?
Related
I am trying to deploy an ear file to a Weblogic 12 server. The ear file contains a HttpServlet. During deployment, the HttpServlet is trying to initialize and fails with this error:
Target state: deploy failed on Cluster
javax.naming.NameNotFoundException: While trying to lookup
'jdbc.' didn't find subcontext 'jdbc'. Resolved
'' at
weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1180)
I tested the data source connection on the weblogic servers and it is successful.
The data source is defined in a properties file that is being accessed because the datasource name is correct. The webserver URL is also correct.
This is the init() method:
public void init(ServletConfig config) throws ServletException
{
super.init(config);
Context jndiContext = null;
Hashtable ht = new Hashtable();
try
{
PropertyManager.getInstance(PROPS_FILE);
PropertyManager.getInstance();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, PropertyManager.getProperty("WL_WEBSERVER_URL"));
jndiContext = new InitialContext(ht);
ds = (javax.sql.DataSource) jndiContext.lookup(PropertyManager.getProperty("SEC_DATASOURCE"));
}
}
I don't understand why it does not recognize the subcontext of jdbc.
I tried removing jdbc. But the error is a NameNotFoundException without jdbc.
UPDATE
I tried changing the data source name to java:jdbc/scantDS. I received a different NameNotFoundException:
". javax.naming.NameNotFoundException: While
trying to look up /jdbc/scantDS in /app/webapp/Load/421643657.;
remaining name '/jdbc/scantDS' at
weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1180)
The problem was that I was trying to deploy using the wrong port number. I was using the port number that the console application was using and I needed a specific port that has the JNDI.
I´m quite new to Java EE and have a comprehension question about running an application in the application server, in my case Wildfly 10.
My Problem is, that objects apparently keep their state, once they where instantiated on application start up, even if i close and re-open the browser session. In my case, i´m using a bean that tries to open a jdbc connection and writes to the facelet´s messages in case something goes wrong.
So, to test the scenario i shut down the database and started the application. As expected, the error message comes up. Now, after restarting the database, it is still not connecting and giving me the "no database" message. A complete restart of the application server is necessary in order to have the bean start over new.
So the question is, what do i have to do, to re-initialize all involved object instances of my application?
Here is the code of the bean in question:
#Named
#Stateless
public class CoworkerProducer
{
#Inject
private EntityManager em;
#Resource(lookup="java:/JMPostgres")
private DataSource dsJM;
public void addCoworkers(Long projectId)
{
Project managedProject = em.find(Project.class, projectId);
Long jmId = managedProject.getJmId();
try {
Connection con = dsJM.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM employees e, departments d WHERE e.department_id = d.id AND d.project_id = " + jmId);
ResultSet result = ps.executeQuery();
while(result.next()){
Coworker cow = new Coworker();
cow.setProject(managedProject);
em.persist(cow);
}
}
catch (SQLException e) {
final FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "no database!", null));
}
}
}
It's best practice to let the container, in your case Wildfly 10, handle the database connection, You can configure that in the standalone.xml file.
Documentation for configuration
In your case, create a (pooled) Datasource in Wildfly and enable connection validation and connection retries in Wildfly. That way, Wildfly will manage your database connection and reestablish connection if it failed.
Once you installed the MySQL driver in Wildfly, you can create the datasource on the Wildfly admin console (by default it runs on port 9990) or with the JBoss CLI.
Further reading:
Datasource configuration
I'm trying get the data base connection using JNDI Name. I'm sing Websphere application server.
Please find below code.
public class GetDbConnection {
private static Connection getConnection() throws Exception {
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://10.70.52.228:30305/");
Connection connection = null;
try{
Context ctx = new InitialContext(pdEnv);
System.out.println("Initial Context: "+ctx.getNameInNamespace());
DataSource ds = (DataSource) ctx.lookup("jdbc/siperian-mdmtrg01-cmx_ors-ds");
System.out.println("DataSource Object : "+ds.getClass());
connection = ds.getConnection();
System.out.println("Connection established ::" +connection);
}catch(Exception ex){
ex.printStackTrace();
}
return connection; }
I'm getting below error.This is the full print stack trace.
Initial Context: MDMTRG2Cell01/clusters/InformaticaMDM_Cluster
J2CA0036E: An exception occurred while invoking method
setDataSourceProperties on
com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl used by
resource jdbc/siperian-mdmtrg01-cmx_ors-ds :
java.lang.NoClassDefFoundError:
com/ibm/websphere/appprofile/accessintent/AccessIntent
Please help me in this reagrd.
The cause of the problem is that the WebSphere Application Server Java™ virtual machine (JVM) could not find the JDBC JAR file.
Example if we are using DB2 datasource we need DB2 JAR, in websphere classpath that contains the implementation class.
The most likely cause is that the classpath of the JDBC Provider configuration has used a WebSphere Application Server variable, for example, DB2_JDBC_DRIVER_PATH, which has not been set for the corresponding WebSphere Application Server jvm process.
This could also be caused on Unix based servers by the userID not having permission to access the jar that contains the class.
I have created the data source in Websphere server and its name is myDataSource(JNDI name is oracleDS). Now I want to access it through the stand alone application.
I just wrote the bellow code to access the data sorce.
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2809");
Context initialContext = new InitialContext(pdEnv);
DataSource dataSource = (DataSource) initialContext
.lookup("java:comp/env/jdbc/oracleDS");
But when I run the application I am getting the bellow exception.
Aug 6, 2013 11:33:41 PM null null
SEVERE: javaAccessorNotSet
javax.naming.ConfigurationException: Name space accessor for the java: name space has not been set. Possible cause is that the user is specifying a java: URL name in a JNDI Context method call but is not running in a J2EE client or server environment.
Please let me know do I have to do any configuration changes in websphere to access the data source or any other code level changes do I have to do?
Please confirm me can we access the datasource from outside of the container(websphere)?
Update1:
I followd the way which PhilipJ mentioned. Then I am gtting the bellow exception.
SEVERE: Cannot get connection: javax.naming.NameNotFoundException: Context: LTC-VIRTUS-24WZNode01Cell/nodes/LTC-VIRTUS-24WZNode01/servers/server1, name: jdbc/oracleDS: First component in name oracleDS not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
javax.naming.NameNotFoundException: Context: LTC-VIRTUS-24WZNode01Cell/nodes/LTC-VIRTUS-24WZNode01/servers/server1, name: jdbc/oracleDS: First component in name oracleDS not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
at com.ibm.ws.naming.jndicos.CNContextImpl.mapNotFoundException(CNContextImpl.java:4365)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1794)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1749)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookupExt(CNContextImpl.java:1500)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:637)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:165)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)
at javax.naming.InitialContext.lookup(InitialContext.java:436)
at com.nyl.connection.ConnectionUtil.getConnection(ConnectionUtil.java:38)
at com.nyl.main.Main.main(Main.java:9)
Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
at org.omg.CosNaming.NamingContextPackage.NotFoundHelper.read(NotFoundHelper.java:95)
at com.ibm.WsnOptimizedNaming._NamingContextStub.resolve_complete_info(_NamingContextStub.java:506)
at com.ibm.ws.naming.jndicos.CNContextImpl$2.run(CNContextImpl.java:2797)
at com.ibm.ws.naming.jndicos.CNContextImpl$2.run(CNContextImpl.java:2793)
at com.ibm.ws.naming.util.CommonHelpers.retry(CommonHelpers.java:763)
at com.ibm.ws.naming.jndicos.CNContextImpl.cosResolve(CNContextImpl.java:2791)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1790)
... 8 more
Update2:
I found the way to avoid the exception. The code should be bellow,
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2809");
Context initialContext = new InitialContext();
DataSource datasource = (DataSource) initialContext.lookup("oracleDS");
if (datasource != null) {
connection = datasource.getConnection("admin","admin");
} else {
LOGGER.info("Failed to lookup datasource.");
}
But the problem here is I am giving the database credintials to creat the connection. I don't want to give it. Can any one please let me know how to create the connection without giving the database credintials?
I found the solution for this issue.
When we creat the InitialContext we have to set two environment variables correctly as bellow,
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2810");
Context initialContext = new InitialContext(pdEnv);
Note:
We can find the listening port of iiop protocol by using the bellow way,
Login to Webaphere admin console => Server => Server Types => Webspher application servers => Click on the name of the server which we use => Communication => Ports.
Then you can find the port for Port name BOOTSTRAP_ADDRESS.
Working code to create the connection using the datasource
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2810");
Context initialContext = new InitialContext(pdEnv);
DataSource datasource = (DataSource) initialContext.lookup("testDS");
if (datasource != null) {
connection = datasource.getConnection("admin","admin"); // DB credintials
} else {
LOGGER.info("Failed to lookup datasource.");
}
Note: I created the data source in websphere(Version 7) and the JNDI name is testDS
and we have to add com.ibm.ws.ejb.thinclient_7.0.0.jar jar in the class path.
Thanks Sean F and PhilipJ for your support regarding this issue.
DataSource dataSource = (DataSource) initialContext
.lookup("jdbc/oracleDS");
try this way.
look here
I have created java web application in eclipse with database mysql.But now i have to deploy that web application in tomcat server.I know how to deploy web application without databas but need assistance with database.
Thank you in advance.
There are two main ways to obtain JDBC connections within a Java Web Application.
Retrieve a connection from a DataSource registered in a JNDI directory service within the container.
Creating a Connection manually within your application code.
JNDI
Using JNDI requires a connection pool to be created within tomcat. This can be done within the context.xml file in tomcat's config directory.
Example Context.xml Entry
<Resource name="jdbc/EmployeeDB"
auth="Container"
type="javax.sql.DataSource"
username="dbusername"
password="dbpassword"
driverClassName="org.hsql.jdbcDriver"
url="jdbc:HypersonicSQL:database"
maxActive="8"
maxIdle="4"/>
This connection would then be retrieved in your code as follows:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
Manual Creation
Manually creating the connection within your code is simpler, however JNDI is recommended for its portability.
Manual Example
public class MysqlConnect{
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
When deploying for either of these scenarios it is important that you have the appropriate JDBC driver in your classpath.
There are 2 scenarios here:
You define the connection directly in the code using simple JDBC and Class.forName() - in that case you just need to make sure the jar containing the driver is in the classpath and it should work.
This is the preferred method - Define a Datasource on the server and call it in the code by using the JNDI API:
InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup("jdbc/testDS");
conn = ds.getConnection();
in contex.xml using -- tag we can connect to any db