gpt4 book ai didi

tomcat - 在 JPA 中使用 tomcat 的连接池

转载 作者:行者123 更新时间:2023-11-28 21:50:58 25 4
gpt4 key购买 nike

我想在运行在 tomcat 上的 Web 应用程序中使用连接池和 JPA/TopLink。之前我使用 jdbc 的连接和语句类来操作数据库中的数据;要以这种方式使用连接池,我只需在 context.xml 中声明资源并在应用程序中获取此资源:

Context c = new InitialContext();
DataSource source = (DataSource) ((Context)c.lookup("java:comp/env")).lookup("jdbc/MySource");

现在我想在 JPA 中使用这样的连接池。我该怎么做?

还有一个问题:我在一些例子中看到,资源是在context.xml中声明的,然后在 中的web.xml中声明。为什么我要在不同的地方声明它还是同一个声明,我的意思是它是否等同于context.xml中的声明?

最佳答案

And now I want to use such connection pool with JPA. How can i do this?

假设您已经通过<Resource> 声明了连接池数据源的创建|在context.xml , 然后你只需要在 webapp 的 /META-INF/persistence.xml 中声明 JPA 的数据源使用.

<persistence-unit name="YourPersistenceUnit" transaction-type="JTA">
<jta-data-source>jdbc/MySource</jta-data-source>
</persistence-unit>

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?

<Resource>一个在context.xml定义 servlet 容器对数据源的创建。它可以被多个 webapps 使用。 <resource-ref>一个在web.xml定义特定网络应用对数据源的使用。注意:使用 JPA 时,不需要 web.xml 中的那个.它进入 persistence.xml .

关于tomcat - 在 JPA 中使用 tomcat 的连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6294127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com