I'm developing a web application and it is to be deployed on Apache Tomcat 6.0. Application will be connecting a lot of databases (almost 25) so in order to manage the Connections, I'm using a context.xml file located under META-INF. So far so good, and here is what a Resource Definition looks like:
<Resource
name="jdbc/XX"
auth="Container"
type="javax.sql.DataSource"
username="XXX"
password="XXX"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:XXXX"
maxActive="8"
maxIdle="4"/>
Given this definition, a Connection object is created correctly.
What I'd like to know is if I am able to add extra information in this definition, such as projectName, and reach it from the context(or somewhere else). Something like the following:
<Resource
name="jdbc/XX"
auth="Container"
type="javax.sql.DataSource"
username="XXX"
password="XXX"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:XXXX"
maxActive="8"
maxIdle="4"
projectName="Cool Project"/>
Any help is appreciated..
No, but you could define a naming convention and add an Environment element for each of your resource :
<Environment name="XX_projectName"
value="Cool Project"
type="java.lang.String"
override="false"/>
In your code, you would access it via
Context ctx = new InitialContext();
String projectName = (String) ctx.lookup("java:comp/env/XX_projectName");
See http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Environment%20Entries for details.
Related
I am using java 7, and tomcat 7. I am writing few tests for my application in jUnit which uses tomcat/conf/server.xml for jndi. Here is the maven suggest folder structure.
src
|___test
|___java
| |___Testcase.java
|___resources
|___conf
|___server.xml
My sample server.xml would look like this,
<Resource name="jdbc/junit_db"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/junit_db?zeroDateTimeBehavior=round&autoReconnect=true&dumpQueriesOnException=true"
username="root"
password="password"
maxIdle="0"
minIdle="0"
initialSize="1"
maxWait="5000"
maxActive="50"
loginTimeout="1000"
minEvictableIdleTimeMillis="2000"
timeBetweenEvictionRunsMillis="5000"
validationQuery="SELECT 1"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="false"
logAbandoned="true"
removeAbandoned="true"
poolPreparedStatements="true"
maxOpenPreparedStatements="10000"
accessToUnderlyingConnectionAllowed="false"
defaultAutoCommit="false"
defaultReadOnly="false"
defaultTransactionIsolation="4"/>
<Resource name="jdbc/junit_hive_db"
type="javax.sql.DataSource"
factory="com.office.hive.HiveDataSourceFactory"
driverClassName="org.apache.hive.jdbc.HiveDriver"
url="jdbc:hive2://localhost:10000/default?zeroDateTimeBehavior=round"
username=""
password="" />
I want to load this server.xml into the IntialContext before running jUnit test cases. How to achieve this?
Followed this link, it has solution for loading jndi into initialcontext manually.
http://www.alexecollins.com/tomcat-context-junit-rule/
Give TomcatJNDI a try. When fed with Tomcat's configuration files it will deliver all JNDI based objects that are declared in these files as soon as they are looked up. The code to achieve this is for example
TomcatJNDI tomcatJNDI = new TomcatJNDI();
tomcatJNDI.processServerXml(serverXmlFile)
tomcatJNDI.processContextXml(contextXmlFile);
tomcatJNDI.start();
Then you can lookup the objects as you are used to:
DataSource ds = (DataSource) InitialContext.doLookup("java:comp/env/path/to/datasource")
More about TomcatJNDI can be found here.
I am new to weblogic server.I have setup JNDI with tomcat server and it runing fine for me .But i have to setup JNDI with weblogic so please anyone help me where i have put
<ResourceLink global="jdbc/myPath" name="jdbc/myPath"
type="oracle.jdbc.pool.OracleDataSource" />
This code i.e in tomcat i have put this code in Context.xml file but not know where to add this in weblogic and this code also:
<Resource name="jdbc/myPath" auth="Container"
type="oracle.jdbc.pool.OracleDataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory" url="url"
user="username" password="password" maxActive="20" maxIdle="10"
maxWait="10000" />
This code is in server.xml file of tomcat but not know where to put in weblogic.Please help me
Thanks
You go to http://localhost:7001/console/. The default credentials are weblogic/welcome1. Then you access the data sources section and create your data source.
I am working on a jsp project in which login form contains three fileds as customer name , user name and password. In this case my mysql database name same as user inputted customer name. In tomcat/conf server.xml file contains JNDI info as below
<GlobalNamingResources>
Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/customer1"/>
and tomcat/conf context.xml file contain below code
<ResourceLink name="jdbc/TestDB"
global="jdbc/TestDB"
type="javax.sql.DataSource"/>
My problem is that when customer login with different name then I want to update my server.xml file url like jdbc:mysql://localhost:3306/customerXXX? I have tried using tomcat PoolProperties but it didn't works, how I will change or update server.xml info or there other any ways I can achieve this.
How do i connect to derby database (that comes with the netbeans) ? I am using Tomcat as the server. Earlier i used the following statements to connect to the derby database,but then i used glassfish as the server.
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/PollDatasource");
Connection connection = ds.getConnection();
But now using Tomcat as the server i am unaware how to do this.
Note : Tomcat and Derby are pre installed with netbeans IDE that i am using currently
In Tomcat find conf/context.xml, then edit and write something like this:
<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
driverClassName="com.YourDriver"
url="jdbc:derby://localhost:1527/nameOfTheDatabase;create=true"
username="username" password="password" maxActive="20"
maxIdle="10" maxWait="-1" />
Note 1: With the above URL the driver will be org.apache.derby.jdbc.ClientDriver
Note 2 : You can also add the above information in META-INF/context.xml of your project. This becomes application specific.If you add the information in tomcat's context.xml that becomes global.
Note 3: Download the jar from this website.Download db-derby-10.9.1.0-bin.zip.It contains many files, including derby.jar and derbyclient.jar (along with much documentation).derbyclient.jar contains our friend org.apache.derby.jdbc.ClientDriver.class. derby.jar contains org.apache.derby.jdbc.EmbeddedDriver. Keep the downloaded jar in lib folder of Tomcat.
and in your application web.xml "resource-ref":
<resource-ref>
<description>my connection</description>
<res-ref-name>jdbc/PollDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
You may want to look at these questions :
Isn't it necessary to mention the name of archive in the Resource tag?
When is the tag I added in context.xml gets read?
What are steps followed in the look-up? what is looked first web.xml or context.xml?
You need to:
1) Copy your derbyclient-*.jar to ${TOMCAT_HOME}/lib.
2) Edit your server.xml and add the following lines to the section GlobalNamingResources:
<Resource auth="Container"
driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
maxActive="8" maxIdle="4"
name="jdbc/my-ds" type="javax.sql.DataSource"
url="jdbc:derby:mydb;create=true"
username="myuser" password="mypassword" />
3) In your context definition, add:
<Context docBase="myapp"
path="/myapp"
reloadable="true"
...>
<ResourceLink name="jdbc/my-ds"
global="jdbc/my-ds"
type="javax.sql.DataSource" />
</Context>
4) Restart Tomcat.
The example you have requires JNDI. See the relevant tomcat versions docs on setting that up.
Or use a connection string, here's a page from derby docs http://db.apache.org/derby/integrate/plugin_help/derby_app.html
I have developed an application on Tomcat 7.0 that uses Datanucleus / JDO to access to a database. I currently have the JDO connection properties stored in the "datanucleus.properties" located in the application itself. The connection is working fine, but I would like to store the connection information as JNDI, to have it on the server and no longer in the war itself (I always have to replace the file in the war when deploying it remotely).
I tried the following:
Create a in the web.xml of the application (jdbc/ConnectionDB)
In "Server.xml", I tried to add the following the context of my application
<Resource name="jdbc/ConnectionDB" auth="Container" type="javax.jdo.PersistenceManagerFactory" /> <ResourceParams name="jdbc/ConnectionDB
<parameter>
<name>javax.jdo.PersistenceManagerFactoryClass</name>
<value>org.datanucleus.api.jdo.JDOPersistenceManagerFactory</value>
</parameter>
<parameter>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost/TomcatTest</value>
</parameter>
...
I then try to create a new PMF with the following syntax:
Context context = null;
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("java:comp/env/jdbc/ConnectionDB",context);
When I run my application, I get a javax.jdo.JDOUserException: You have either specified for this PMF to use a "persistence-unit" of "datanucleus.properties" (yet this doesnt exist!)
I don't really understand what is wrong in my setup.
Regards,
Marcel
I finally found the solution I was looking for, I post it here, it might help somebody else:
Create a resource in "Context.xml" file of the server
<Resource name="jdbc/SyncTestDB"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="mysql"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/SyncTestDB"/>
Create a reference to that resource in the "web.xml" file of your application
<resource-ref>
<description>MySQL Database Connection</description>
<res-ref-name>jdbc/SyncTestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And finally get a Persistence Manager Factory using the JNDI connection:
PersistenceManagerFactory pmf;
Properties properties = new Properties();
properties.setProperty("datanucleus.ConnectionFactoryName","java:comp/env/jdbc/SyncTestDB");
Read the javadoc for JDOHelper.getPersistenceManagerFactory(String) and it is obviously not for passing in some JNDI data source string.
Read the docs for Tomcat and you will also see that specifying a datasource you do not provide JDO connection details.
You can equally specify a persistence.xml with that JNDI string for the "javax.jdo.option.ConnectionFactoryName" property. As per the JDO spec and DataNucleus/Tomcat docs then