[英]Jetty and SQLite connection in servlets
我正在使用Java中的一个小型servlet应用程序,使用Maven,在Netbeans中。 我正在使用SQLite数据库和最新的Jetty服务器。 我在创建用于servlet的连接池时遇到问题。 什么有效(在servlet中):
Class.forName("org.sqlite.JDBC");
String url = "jdbc:sqlite:c:\\db\\base";
Connection con = DriverManager.getConnection(url);
这在没有jetty.xml / web.xml / pom.xml中的特定设置的情况下工作我有库使用SQLite和连接池(org.xerial.sqlite-jdbc,commons-pool,commons-dbcp)。
什么行不通:
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 - 依赖项。
即使没有更改代码(更改为使用池),此配置也会在jetty控制台中创建错误: 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) ...
在Jetty中打开应用程序:错误503
我认为我在jetty.xml中声明jdbc / DSTestPool有问题,我尝试了不同的参数,但结果是一样的。
您的jetty.xml中缺少一个参数,您可以声明以下任何类型:
每种类型都遵循相同的模式:
<New class="org.eclipse.jetty.plus.jndi.xxxx">
<Arg><!-- scope --></Arg>
<Arg><!-- name --></Arg>
<Arg><!-- value --></Arg>
</New>
您的jetty.xml文件具有名称和值,但缺少范围。 尝试使用以下JVM实例范围(该名称在整个JVM实例中是唯一的)
<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>
或者类似于Web应用程序上下文范围的以下内容(该名称对于WebAppContext实例是唯一的)
<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>
请参阅此处的文档了解详细信息
一种方法可能是使用HikariCp(或任何其他pooler ..)。 在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>
在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>
然后你得到数据源:
(DataSource)new InitialContext().lookup("java:/comp/env/jdbc/ds");
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.