I'm developing java web application using tomcat 7
I want to have context.xml file outside of my war with custom name. I have database resource defined in this file.
According to tomcat documentation, I put context.xml.default file in CATALINA_HOME/conf/[enginename]/[hostname]/ directrory and I could get database configuration.
Now, I want to rename context.xml.default and put custom file name,which matches my contextpath. If I do so, I can't get database configuration anymore and my appliction throws Exception:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
Here is the content o my context.xml.default file:
<Context
crossContext="true"
reloadable="true" >
<Resource
name="jdbc/myDS"
auth="Container"
driverClassName="oracle.jdbc.driver.OracleDriver"
factory="com...encription.util.EncryptedDataSourceFactory"
logAbandoned="true"
maxActive="30"
maxIdle="10"
maxWait="1000"
password="encryptedPass"
removeAbandoned="true"
removeAbandonedTimeout="60"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:#myIP:myPort:FRONT"
username="username" />
The configuration I used was correct but the problem was that eclipse was using {catalina_base}/wtpwebapps directory to deploy web applications.
I changed wtpwebapps folder with webapps in server configuration in eclipse and now everything works correctly.
The error might be a result of a missing JDBC driver. Try putting the Oracle driver into the %TOMCAT_HOME%/common/lib directory.
Another possibility is, that you are missing a web-application specific context.xml (named <NAME_OF_WEBAPP>.xml) in conf/catalina/localhost.
Related
I have tomcat's context.xml file with references to the context.properties file
<Context sessionCookiePath="/" path="/">
<Resource name="jdbc/dbwriter" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
type="javax.sql.DataSource"
url="jdbc:mysql://${context.ip}:3306/test" username="${user}"
password="${pass}" />
</Context>
The context.properties file looks like:
context.ip=localhost
user=admin
pass=admin
But it seems that tomcat does not load the properties file and tries to resolve jdbc:mysql://${context.ip}:3306. What am I doing wrong?
For build I am using maven. Maybe it should fill up the property placeholders?
EDIT: the context.properties file is located in side WEB-INF/classes folder, and context.xml is located in the META-INF folder inside the WAR file.
There is little point shipping your context.properties file as part of the web application. You might as well have the build system do the property replacement for you.
If you want to keep the property definitions, Tomcat will pick up those properties if you add them to the catalina.properties file located in $CATALINA_BASE/conf
I'm working on a Java web application in Eclipse and deploying to an instance of Tomcat that is run by Eclipse. I'm trying to get this application to talk to a database on another host via a JNDI Resource element.
The context.xml file included in the application attempts to connect to a MySQL server running on localhost, like so:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="log4j.configuration"
value="/path/to/installed/log4j.properties"
type="java.lang.String" />
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
<Environment name="mykey" value="installed" type="java.lang.String" />
</Context>
This configuration works when I install it on the actual host. That is, it reads the environment values correctly and connects to the database.
I can also install this on a separate host and edit /etc/tomcat6/conf/Catalina/localhost/myaplication.xml to change the JDBC URL, like so:
...
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://otherhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
...
This configuration also works. In other words, I'm pretty confident that my application is using the JNDI correctly.
So, now I want to deploy this to Tomcat on my desktop in Eclipse. I've added the project to Tomcat in the standard way, and I've edited the context.xml file for the server (i.e., the one that Eclipse shows under my Tomcat server in the "Servers" project) like so:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="log4j.configuration"
value=C:\workspace\myapplication\src\main\resources\conf\log_to_tomcat_console_log4j.properties"
type="java.lang.String" />
<Resource name="jdbc/configDB" auth="Container" type="javax.sql.DataSource"
username="dbuser" password="dbpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://otherhost:3306/dbname" maxActive="8"
validationQuery="SELECT 1" testOnBorrow="true" />
<Environment name="mykey" value="desktop" type="java.lang.String" />
</Context>
When I then restart Tomcat in Eclipse, log shows me that the path to the Log4J file is being read correctly (because it starts logging to the Tomcat console) and I even see it spit out the correct "mykey" value. However, initialisation fails when it tries to read from the configDB.
At first, I thought this was a connectivity issue, but after ruling that out, I restarted Tomcat in the debugger and inspected the DataSource read from JNDI; its URL points to localhost, meaning it's using the one in the application's context.xml file, not Tomcat's!
I've tried several ways to get this working, including putting this information in Tomcat's server.xml file, but nothing seems to work. Or, more accurately, it sort of kind of works because it's reading the Environment values but not the Resource.
What am I doing wrong here? How do I get Eclipse's Tomcat server to override this Resource?
The basic problem here is, that the context.xml you edit in eclipse corresponds to the main context.xml in the conf directory of tomcat. And then the preferences in the application context file takes precedence. As you already described within normal deployment lifecycle of tomcat this is perfectly fine, because you just edit the application settings.
You could do the same with the context file created by eclipse (which is located somewhere at .metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/Catalina/localhost) but it will be overwritten as soon as you change something in the original context file (META-INF/context.xml).
So actually you would need to make the changes there, but this is normally out of question because this file must be deployed without custom changes.
But here is a workaround: You can make eclipse use another META-INF/context.xml by abusing the deployment assembly. Make a new META-INF directory somewhere else and place a context.xml file with your setting there. Then in the project properties of your eclipse project at Deployment Assembly add an additional mapping from your new path/to/META-INF to META-INF.
Then eclipse will overwrite the original context.xml with your custom one and tomcat should take your settings.
When trying to access a servlet that reads from the database i get the following error:
HTTP Status 500 -
...
java.lang.NullPointerException
com.pdq.sms.db.DatabaseAccess.getConnection(DatabaseAccess.java:39)
com.pdq.sms.db.DataRow.Read(DataRow.java:79)
com.kessel.controllers.Home.doPost(Home.java:50)
com.kessel.controllers.Home.doGet(Home.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.26 logs.
Looking in the Tomcat log files i find this : "Name testapp is not bound in this Context"
This is the relevant part of the server.xml
<Host name="myserver.org" appBase="webapps/test"
unpackWARs="true" autoDeploy="true">
<Context path="/" docBase="testapp" reloadable="false">
<Resource name="jdbc/testapp" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="10" maxWait="10000"
validationQuery="SELECT 1" driverClassName="com.mysql.jdbc.Driver"
username="myuser" password="mypass"
url="jdbc:mysql://localhost:3306/test_db?zeroDateTimeBehavior=convertToNull" />
</Context>"
</Host>"
I've also modified the 04webapps.policy to give the application permission to connect:
permission java.net.SocketPermission "127.0.0.1:3306", "resolve, connect";
User is setup properly on the MySQL server, i can manually connect and select, insert etc on the database in question.
What am i not doing right ?
Change your resourceName ;
java:comp/env/jdbc/testapp
to
java://comp/env/jdbc/testapp
First write a jdbc code as a simple java proagram and don't forget to add the jdbc driver as external jar file.
And you can also try by putting the jdbc jar file in tomcat lib folder or jre/lib/ext folder, this is because when a servlet compiles it will take help of lib jar file in cse it in a 3rd party jar. if the jdbc connectivity code has no error it will run file after adding the jar file
I am using Tomcat and Java (through Eclipse) and SQL Server 2008 R2
I am getting
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of
class '' for connect URL 'null'
I also tried deploying to Tomcat 7 and I got exactly the same error. (I made sure that in this case the sqljdbc4.jar was in the lib directory of Tomcat, rather than just in the WAR)
Can anyone help? Thanks!
My code is as follows:
Server.xml
<GlobalNamingResources>
<Resource type="javax.sql.DataSource" auth="Container"
name="jdbc/charmDB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;databaseName=CH;
integratedSecurity=true"
/>
Web.xml
<resource-ref>
<description>CH Database</description>
<res-ref-name>jdbc/charmDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Java code
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/charmDB");
Connection con = ds.getConnection();
Resources declared in the the GlobalNamingResources will not be visable by your web application unless you link them to the application's context.
Otherwise, you could define the resource in the default context or the web application's context.
Ive tried the above. Just wanted to add also. You can add in server.xml also, but ensure you are not using Resource-Params. Put the parameters in the Resource Element only.
If you are on linux then ensure sqlodbc4.jar is placed in tomcat/lib found under /usr/share folder.
I have a web application and use tomcat-5.5.27, eclipse 3.4 on open SUSE. I use it from eclipse (deploy, run, debug, etc...)
I have to set some JNDI resources (datasources) that Tomcat will expose them.
These resources are defined now in \META-INF\context.xml. This works fine.
But, when I move this file (context.xml) in tomcat-5.5.27/conf/context.xml or
tomcat-5.5.27/conf/Catalina/localhost/myapp.xml, it does not load the resources defined in the file.
What can be the problem?
My context.xml:
<Context path="/myapp" docBase="/myapp" debug="5" reloadable="true" crossContext="true">
<Resource name="jndi_name" auth="Container"
type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="COM.ibm.db2.jdbc.app.DB2Driver"
url="jdbc:path_to_db"
username="username"
password="password"
validationQuery="SELECT 1 FROM SYSIBM.SYSDUMMY1"
defaultTransactionIsolation="READ_COMMITTED"
removeAbandoned="true" logAbandoned="true" >
</Context>
If you're using the web tools project feature in Eclipse to manager your Tomcat instance you need to know that it does change Tomcat base directories.
Try using a separate Tomcat install and see if that helps.
This could be a classpath issue when tomcat comes to load the JDBC driver, (ie. before it loads your webapp).
Do you have the JAR file for the DB2 driver in tomcat-5.5.27/common/lib ? It must be there for
the class loader to find it, and not in your webapp's WEB-INF/lib directory.
where is your webapp stored ? It's own folder or under webapps ? check if your docBase is valid, if your app is under webapps I don't think you need the "/"
Any relevant info from the Tomcat logs ?
Open the tomcat server in the server view, modify anything like the timeout, save it and your context will be recognized.
What do the tomcat logs show when you start it up?
That tag is not closed, check if you copied it to question correctly.