Configuring Jetty 6 to use commons.dbcp datasource - java

Hi I'm trying to configure Jetty 6.1.26 to use connection pooling and it's giving me a hard time.
I put commons-dbcp-1.4.jar, commons-pool-1.5.6.jar and mysql-connector-java-5.1.16 in
my Jetty/lib/ext folder.
I also added references to those jars in my Jetty/pom.xml
<dependencies>
...
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>
</dependencies>
In my web project in eclipse, my jetty-env.xml (in WEB-INF) file is like this:
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New id="MySQLDB" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>MySQLDB</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
<Set name="url">jdbc:mysql://host_ip</Set>
<Set name="username">user</Set>
<Set name="password">pwd</Set>
<Set name="auth">Container</Set>
<Set name="maxActive">-1</Set>
<Set name="maxIdle">30</Set>
<Set name="maxWait">10000</Set>
<Set name="minEvictableIdleTimeMillis">600000</Set>
<Set name="name">MySQLDB</Set>
<Set name="removeAbandoned">true</Set>
<Set name="removeAbandonedTimeout">5000</Set>
<Set name="timeBetweenEvictionRunsMillis">10000</Set>
<Set name="type">javax.sql.DataSource</Set>
</New>
</Arg>
</Configure>
However, when I start Jetty (using java -jar start.jar in my Jetty directory), I get this exception:
java.lang.NoSuchMethodException: class org.apache.commons.dbcp.BasicDataSource.setAuth(class java.lang.String)
How can I setup Jetty correctly?
Thanks alot!

In your code you have <Set name="auth">Container</Set> the instruction says to call the method setAuth of the class. But the class doesn't have anything like that.

Remove the lines <Set name="auth">Container</Set> and <Set name="type">javax.sql.DataSource</Set> from the configuration: the exception is telling you that those functions don't exist on the org.apache.commons.dbcp.BasicDataSource class.

Related

Jetty - connection pool for different contexts and database, but same lookup code

I'm trying to configure the connection pool 2 web applications using .xml context descriptor for 2 separate context paths and databases, but with the same logical code.
The problem is that when using JNDI to lookup the datasource, the resources are overridden when jetty loads the context, and the apps end up using the same database. Here is my configuration and code:
Configuration for MyApp1.xml:
<New id="myDataBase" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/myDataBase</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://host:3306/mydb</Set>
<Set name="User">...</Set>
<Set name="Password">...</Set>
</New>
</Arg>
</New>
Configuration for MyApp2.xml
<New id="myDataBase" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/myDataBase</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://host:3306/mydb-test2</Set>
<Set name="User">...</Set>
<Set name="Password">...</Set>
</New>
</Arg>
</New>
Code used by the apps:
public class MyDb {
...
static {
try {
datasource = (DataSource) new InitialContext().lookup("jdbc/myDataBase");
} catch (NamingException e) {
// ...
}
...
}
So, jetty loads context for MyApp1.xml, then MyApp2.xml. When I try to access the apps, they both use the same database: mydb-test2.
How can I properly resolve this problem?
EDIT:
I have tried adding the argument in the configuration but it is not working; it is strange that now the contexts both use the first configured database: mydb
This is the xml for each app:
<Configure id='wac1' class="org.eclipse.jetty.webapp.WebAppContext">
...
<New id="myDataBase" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid='wac1'/></Arg>
<Arg>jdbc/myDataBase</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://host:3306/mydb</Set>
<Set name="User">...</Set>
<Set name="Password">...</Set>
</New>
</Arg>
</New>
...
<Configure id='wac2' class="org.eclipse.jetty.webapp.WebAppContext">
...
<New id="myDataBase" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid='wac2'/></Arg>
<Arg>jdbc/myDataBase</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://host:3306/mydb-test2</Set>
<Set name="User">...</Set>
<Set name="Password">...</Set>
</New>
</Arg>
</New>
...
What am I missing? Do I have to change the code (way in which I make the lookup)?
I've tried
datasource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/myDataBase");
but I get a javax.naming.NameNotFoundException - probably because I dont use env variables, but I don't think I need that, just per context scope lookup)
EDIT 2:
Ok, so I was actually missing a web.xml config which solved the issue (see http://www.eclipse.org/jetty/documentation/current/jndi-configuration.html#configuring-datasources)
<resource-ref>
<res-ref-name>jdbc/HugaDataBase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
You need to set the scope for the JNDI entries. Instead of the empty first argument, you should have something like this:<Arg><Ref refid='myapp'/></Arg>

How configure jetty-maven-plugin 9 to use https?

I need to use https for jetty maven plugin.
I googled following answer
https://stackoverflow.com/a/3795116/2674303
But looks like this answer is not suitable for jetty-maven-plugin of 9 version.
idea complains about syntax
How to fix my problem?
Ok, since jetty-9.0 it is no longer possible to configure a https connector directly in the pom.xml: you need to use jetty xml config files to do it.
I am new to exchange ,so excuse my code copy/paste.
jetty.xml
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Set name="secureScheme">https</Set>
<Set name="securePort"><Property name="jetty.secure.port" default="8443" /></Set>
<Set name="outputBufferSize">32768</Set>
<Set name="requestHeaderSize">8192</Set>
<Set name="responseHeaderSize">8192</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">false</Set>
<Set name="headerCacheSize">512</Set>
<!-- Uncomment to enable handling of X-Forwarded- style headers
<Call name="addCustomizer">
<Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/>
</Arg>
</Call>
-->
</New>
Look here, for whole bunch of xml's and if it is helpful please check my answer as correct

Jetty and SQLite connection in servlets

I'm making a small servlet app in Java, with Maven, in Netbeans. I'm using SQLite database, and newest Jetty server. I have problem with creating connection pool for use in servlets. What works(in servlet):
Class.forName("org.sqlite.JDBC");
String url = "jdbc:sqlite:c:\\db\\base";
Connection con = DriverManager.getConnection(url);
And this works without specific settings in jetty.xml/web.xml/pom.xml
I have libraries to use SQLite and connection pools (org.xerial.sqlite-jdbc, commons-pool, commons-dbcp).
What doesn't work:
web.xml:
<resource-ref>
<description>DB Connection Pool</description>
<res-ref-name>jdbc/DSTestPool</res-ref-name>
<res-type>javax.sql.ConnectionPoolDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
jetty.xml
<New id="DSTestPool" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/DSTestPool</Arg>
<Arg>
<New class="org.sqlite.SQLiteConnectionPoolDataSource">
<Set name="driverClassName">org.sqlite.JDBC</Set>
<Set name="url">jdbc:sqlite:c:\\db\\base</Set>
</New>
</Arg>
</New>
pom.xml - dependencies.
This configuration, even without changes in code (change to using pool) creates error in jetty console:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
....
Caused by:
java.lang.IllegalStateException: Nothing to bind for name jdbc/DSTestPool at org.eclipse.jetty.plus.webapp.PlusDescriptorProcessor.bindEntry(PlusDescriptorProcessor.java:895)
...
Opening app in Jetty: Error 503
I think there is something wrong with my declaration of jdbc/DSTestPool in jetty.xml, I've tried different parameters, but result was the same.
There is one parameter missing in your jetty.xml, you can declare any of the following types:
org.eclipse.jetty.plus.jndi.EnvEntry: for env-entry type of entries
org.eclipse.jetty.plus.jndi.Resource: for all other type of resources
org.eclipse.jetty.plus.jndi.Transaction: for a JTA manager
org.eclipse.jetty.plus.jndi.Link: for link between a web.xml resource name and a naming entry
And each of these types follow the same pattern:
<New class="org.eclipse.jetty.plus.jndi.xxxx">
<Arg><!-- scope --></Arg>
<Arg><!-- name --></Arg>
<Arg><!-- value --></Arg>
</New>
Your jetty.xml file have the name and value but the scope is missing.
Try with the following for JVM instance scope (the name is unique across the JVM instance)
<New id="DSTestPool" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg><!-- empty arg -->
<Arg>jdbc/DSTestPool</Arg>
<Arg>
<New class="org.sqlite.SQLiteConnectionPoolDataSource">
<Set name="driverClassName">org.sqlite.JDBC</Set>
<Set name="url">jdbc:sqlite:c:\\db\\base</Set>
</New>
</Arg>
</New>
Or something like the following for a web app context scope (the name is unique to the WebAppContext instance)
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DSTestPool" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid='wac'/></Arg><!-- reference to WebAppContext -->
<Arg>jdbc/DSTestPool</Arg>
<Arg>
<New class="org.sqlite.SQLiteConnectionPoolDataSource">
<Set name="driverClassName">org.sqlite.JDBC</Set>
<Set name="url">jdbc:sqlite:c:\\db\\base</Set>
</New>
</Arg>
</New>
</Configure>
See the documentation here for details.
An approach could be to use HikariCp (or any other pooler..).
In jetty-env.xml:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/ds</Arg>
<Arg>
<New class="com.zaxxer.hikari.HikariDataSource">
<Arg>
<New class="com.zaxxer.hikari.HikariConfig">
<Set name="dataSourceClassName">org.sqlite.SQLiteDataSource</Set>
<Call name="addDataSourceProperty">
<Arg>url</Arg>
<Arg>jdbc:sqlite:path/to/test.db</Arg>
</Call>
</New>
</Arg>
</New>
</Arg>
In web.xml:
<resource-ref id="ds">
<res-ref-name>jdbc/ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
then you get datasource:
(DataSource)new InitialContext().lookup("java:/comp/env/jdbc/ds");
...

Jetty and custom sessionIdManager

In order to have custom session storage I have implemented a custom sessionManager (by extending NoSqlSessionManager) and sessionIdManager. My code (along with jars it requires) went into ${jetty.home}/lib/ext (version 8.1.4 BTW). With start.ini i included another config file with following content:
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Set name="sessionIdManager">
<New id="customIdMgr" class="com.me.customSessionIdManager">
<Arg>...</Arg>
</New>
</Set>
</Configure>
Jetty starts and sessionIdManager appears to be working. At least scavenge() method is being called. So far so good. Next step is to associate my custom sessionManager with the WebAppContext of my choice. I did it within the overlay template (overlay.xml) with following content:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Get name="server">
<Get id="customIdMgr" name="sessionIdManager"/>
</Get>
<Set name="sessionHandler">
<New class="org.eclipse.jetty.server.session.SessionHandler">
<Arg>
<New class="com.me.customSessionManager">
<Set name="sessionIdManager"><Ref id="customIdMgr"/></Set>
</New>
</Arg>
</New>
</Set>
</Configure>
However when starting Jetty I get this:
WARN:oejx.XmlConfiguration:Config error at <Get id="customIdMgr" name="sessionIdManager"/> java.lang.NullPointerException
...which implies that the object that was registered in the main jetty configuration is now gone when overlays are processed.
Any idea what I'm doing wrong here?
After quite a lot of debugging it turns out, the root of the problem is <Get name="server"> returns null. Most probably reference to Server class is injected into WebAppContext much later in the deployment process. So instead of getting Server i referenced it with <Ref id="Server"> and that did the trick.

Defining two data sources in jetty (jetty-env.xml)

I'm trying to define two data sources in my web application, using the jetty-env.xml file.
It works ok with just one data source, however I get this exception when the second data source is added:
java.lang.IllegalStateException: Nothing to bind for name javax.sql.DataSource/default
Here's my configuration:
jetty-env.xml
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="ds" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/mybd1</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
<Set name="jdbcUrl">jdbc:jtds:sqlserver://url:1433/mybd1</Set>
<Set name="user">xx</Set>
<Set name="password">yy</Set>
</New>
</Arg>
</New>
<New id="ds2" class="org.eclipse.jetty.plus.jndi.Resource" >
<Arg>jdbc/mybd2</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
<Set name="jdbcUrl">jdbc:jtds:sqlserver://url:1433/mybd2</Set>
<Set name="user">xx</Set>
<Set name="password">yy</Set>
</New>
</Arg>
</New>
</Configure>
web.xml
<resource-ref>
<res-ref-name>jdbc/mybd1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>jdbc/mybd2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
hibernate.cfg.xml (there is another hibernate.cfb.xml to configure the second data source)
<session-factory>
<property name="connection.datasource">jdbc/mybd1</property>
<!-- ... -->
Any clue?
I haven't had a chance to test it, but it looks to me like your problem is that you're missing an <Arg /> for the scope.
Your DS should be:
<New id="ds" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/mybd1</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
etc.
That first "Arg" is the scope, and without it, the rest of your arguments are out of position, and are probably causing your issue.
The id parameter values should match in jetty-env.xml and web.xml
jetty-env.xml
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DS1" class="org.eclipse.jetty.plus.jndi.Resource">...</New>
<New id="DS2" class="org.eclipse.jetty.plus.jndi.Resource">...</New>
</Configure>
web.xml
<resource-ref id="DS1">...</resource-ref>
<resource-ref id="DS2">...</resource-ref>
Try to enable logging in Jetty.
Be carefull logger name is "jndi".
Jetty developers don't use class-name as a logger-name for JNDI.
I spent 2 days to finding difference between name defined in web.xml and jetty-env.xml.
Take a look in :
https://www.eclipse.org/jetty/documentation/9.4.x/using-jetty-jndi.html
Deciding Where to Declare Resources
You can define naming resources in three places:
jetty.xml
Naming resources defined in a jetty.xml file are scoped at either the JVM level or the Server level.
The classes for the resource must be visible at the Jetty container level. If the classes for the resource only exist inside your webapp, you must declare it in a WEB-INF/jetty-env.xml file.
WEB-INF/jetty-env.xml
Naming resources in a WEB-INF/jetty-env.xml file are scoped to the web app in which the file resides. While you can enter JVM or Server scopes if you choose, we do not recommend doing so. The resources defined here may use classes from inside your webapp. This is a Jetty-specific mechanism.
Context xml file
Entries in a context xml file should be scoped at the level of the webapp to which they apply, although you can supply a less strict scoping level of Server or JVM if you choose. As with resources declared in a jetty.xml file, classes associated with the resource must be visible on the container’s classpath.
And put a file like this :
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<!-- Add an EnvEntry only valid for this webapp -->
<New id="gargle" class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg>gargle</Arg>
<Arg type="java.lang.Double">100</Arg>
<Arg type="boolean">true</Arg>
</New>
<!-- Add an override for a global EnvEntry -->
<New id="wiggle" class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg>wiggle</Arg>
<Arg type="java.lang.Double">55.0</Arg>
<Arg type="boolean">true</Arg>
</New>
<!-- an XADataSource -->
<New id="mydatasource99" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/mydatasource99</Arg>
<Arg>
<New class="com.atomikos.jdbc.SimpleDataSourceBean">
<Set name="xaDataSourceClassName">org.apache.derby.jdbc.EmbeddedXADataSource</Set>
<Set name="xaDataSourceProperties">databaseName=testdb99;createDatabase=create</Set>
<Set name="UniqueResourceName">mydatasource99</Set>
</New>
</Arg>
</New>
</Configure>

Categories

Resources