将tomcat的连接池与JPA一起使用 [英] Using tomcat's connection pool with JPA
问题描述
我想在运行于tomcat的Web应用程序中将连接池与JPA/TopLink一起使用.以前,我使用jdbc的Connection和Statement类来处理数据库中的数据.以这种方式使用连接池,我只需在context.xml中声明资源并在应用程序中获取此资源:
I want to use connection pool with JPA/TopLink in my web app running on tomcat.Earlier I use jdbc's Connection and Statement classes to manipulate data in database; to use connection pool in that way I simply declare resource in context.xml and get this resource in application:
Context c = new InitialContext();
DataSource source = (DataSource) ((Context)c.lookup("java:comp/env")).lookup("jdbc/MySource");
现在我想在JPA中使用这样的连接池.我该怎么办?
And now I want to use such connection pool with JPA. How can i do this?
另一个问题:在某些示例中,我看到reosurce在context.xml中声明,然后在<中的web.xml中声明.资源参考>.为什么我应该在不同的地方声明它还是相同的声明,我的意思是它等同于context.xml中的声明?
One more question: I've seen in some examples that the reosurce is declared in context.xml and then it is declared in web.xml in < resource-ref>. Why I should declare it in different places or is it the same declaration, I mean is it an equivalent of declaration in context.xml?
推荐答案
现在我想在JPA中使用这样的连接池.我该怎么办?
假定您已经在context.xml
中声明了<Resource>
创建的连接池数据源,那么您只需在Webapp的/META-INF/persistence.xml
中声明JPA的数据源使用情况.
Assuming that you've already declared the connection pooled datasource creation by <Resource>
in a context.xml
, then you just need to declare the datasource usage for JPA in the webapp's /META-INF/persistence.xml
.
<persistence-unit name="YourPersistenceUnit" transaction-type="JTA">
<jta-data-source>jdbc/MySource</jta-data-source>
</persistence-unit>
另一个问题:在某些示例中,我看到reosurce在
context.xml
中声明,然后在web.xml
中在<resource-ref>
中声明.为什么我应该在不同的地方声明它还是相同的声明,我的意思是它等同于context.xml
中的声明?
One more question: I've seen in some examples that the reosurce is declared in
context.xml
and then it is declared inweb.xml
in<resource-ref>
. Why I should declare it in different places or is it the same declaration, I mean is it an equivalent of declaration incontext.xml
?
context.xml
中的<Resource>
中的一个定义了servlet容器对数据源的创建.它可以被多个Web应用程序使用. web.xml
中的<resource-ref>
一词定义了特定Web应用程序对数据源的使用情况.注意:使用JPA时,不需要web.xml
中的一个.它进入persistence.xml
.
The <Resource>
one in context.xml
definies the creation of the datasource by the servletcontainer. It can be used by multiple webapps. The <resource-ref>
one in web.xml
definies the usage of the datasource by the particular webapp. Note: when using JPA, you don't need the one in web.xml
. It goes into persistence.xml
.
这篇关于将tomcat的连接池与JPA一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!