I've followed another stackover flow thread to get to this point, it was this one here:
Tomcat6 MySql JDBC Datasource configuration
The problem I have is that the line that goes:
Connection conn = ds.getConnection();
from this block:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
Eclipse gives me the error getConnection() is undefined for the type DataSource.
Its solution is to do this:
Connection conn = ((java.sql.Statement) ds).getConnection();
No tutorials show the need to do this, and its not working when I do that. I'm using mySQL jar named, mysql-connector-java-5.1.18-bin I've used it with RMI before but never Tomcat, is it the correct type for use with Tomcat?
TIA
If I look into the Java API docs http://docs.oracle.com/javase/6/docs/api/ I find the javax.sql.DataSource interface with a getConnection() method. I assume your DataSource to be something else than one implementing the javax.sql.DataSource interface. What "DataSource" is imported?
Related
How do i list all connections that are in use?
This is my code:
InitialContext initialContext = new InitialContext();
DataSource dataSource = (DataSource) initialContext.lookup("jdbc/xxxxxx");
con = dataSource.getConnection();
So, having this, i want to list all connections that are currently been used to monitor the system and if needed, close the connections.
You can create a util wrapper where getConnection() will increment and returnConection will decrement. You can use connection pool like dbcp which has BasicDataSource has method getNumIdle() and getNumActive() for this kind of matrics
So, I was exploring on how to use connection pooling on java web application. I looked at some tutorials and I am wondering when should I do the context lookup.
Something like this
initContext = new InitialContext();
dataSource = (DataSource) initContext.lookup( "java:/comp/env/jdbc/mysql");
Should it be everytime I get a connection, or should it be a one time thing.
Obviously initialize connection pool one time only, you can do lookup any time required.
Note: The init-context is not creating connection pool
I am having a slight problem with using JDBC tomcat pool. I have properly defined the resource in context.xml, as well as referring to it in web.xml. Now in my Database access method, I would like to somehow get a data source for when a user get something from the database. However, when I type this in:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/testDB");
I get the error message "InitialContext cannot be resolved to a type". What is the problem here?
Be sure to import InitialContext, it sounds like it is missing to the compiler.
I have created a dynamic web project in WAS 7.0.0.25. I have configured datasource as jdbc/DWLConfig in the WAS. I am trying to lookup this datasource in the servlet from the web project i have created.
if i give java:comp/env/jdbc/DWLConfig, it is giving me NameNotFoundException. But if i give jdbc/DWLConfig, then it is working fine.
Actually, from the servlet, i am calling another project which i dont have access to edit, always looks like java:comp/env/jdbc/DWLConfig. So it is throwing exception for me.
Do i need to add any reference in the web project which i have created.?
The problem might be related to the base JNDI. You will notice why it is not working from the following example:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Right
DataSource ds = (DataSource) envCtx.lookup("jdbc/DWLConfig");
// Wrong. because base JNDI already created.
DataSource ds = (DataSource) envCtx.lookup("java:comp/env/jdbc/DWLConfig");
I am trying to use a datasource that I set up on my weblogic server.
datasource JNDI name = thinOracleDataSource
in my code I have the following
public class DAOBean implements java.io.Serializable {
private Connection conn;
public void connect() throws ClassNotFoundException,
SQLException, NamingException {
Context ctx = new InitialContext();
// Lookup using JNDI name.
DataSource ds = (javax.sql.DataSource) ctx.lookup("thinOracleDataSource");
conn = ds.getConnection();
}
But I get this error
javax.naming.NameNotFoundException: While trying to look up /thinOracleDataSource in /app/webapp/PreAssignment2/24911485.; remaining name '/thinOracleDataSource'
am I looking the JNDI name in the right way? or am I missing something? Thanks for any help!!
EDIT:
This is the jndi tree that I can get from the weblogic console
Try naming your datasource jdbc/thisOracleDataSource in Weblogic and reference it as:
DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/thinOracleDataSource");
Also, make sure the datasource is "targeted" to your weblogic Java server. All of this can be done in the Weblogic admin console.
Your JNDI key should look like approximately "java:comp/env/jdbc/thinOracleDataSource".
You can verify it by using Weblogic console that allows access (and probably search) in JNDI. So, you can check this manually before writing the code.