了解 JPA 中的 Persistence.xml
我试图了解以下事情:
当我创建一个 EJB 项目并将其部署到 Glassfish 时,我是否在 Glassfish 的管理员中心设置 JDBC 资源/连接池,或者我是否在 Glassfish 中添加用户名、密码等的所有不同属性持久性.xml?我一点也不明白。
我也不明白为什么我们同时拥有 JDBC 资源和 JDBC 连接池。它是什么以及它们之间有什么区别?有人可以向我解释这些事情或/并提供一些有关 persistence.xml 文件及其周围部分的良好链接吗?
I am trying to understand the following things:
When I make an EJB project and deploys it to Glassfish do I set up JDBC resources/connection pools at the administrator center of Glassfish or do I add all the different properites for username, password etc in the persistence.xml? I don't understand one bit of that.
I do not understand why we have both JDBC resource and JDBC Connection pool either. What is it and what is the difference between them? Could somebody explain me these things or/and provide some good link about the persistence.xml file and the part around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好定义 JDBC 资源,而不是将信息放在 persistence.xml 中。通过这种方式,您将利用连接池。您将在 persistence.xml 中定义为 JDBC 资源提供的 JNDI 名称。
下面我粘贴了 Glassfish 3.x 帮助的一些部分。一探究竟。这确实很有帮助
JDBC 连接池
JDBC 连接池包含一组 JDBC 连接,这些连接是在注册连接池时创建的
JDBC 资源
Java 数据库连接 (JDBC) 资源(数据源)为应用程序提供连接到数据库的方法。通常,管理员为域中部署的应用程序访问的每个数据库创建一个 JDBC 资源;但是,可以为一个数据库创建多个 JDBC 资源。
应用程序通过在 Java 命名和目录接口 (JNDI) API 树上查找数据源,然后请求连接,从连接池获取数据库连接。与数据源关联的连接池提供与应用程序的连接。
将数据源(JDBC)资源视为某种类型的数据服务(连接池)外观的工厂。在这种情况下,它隐式地从池中获取连接并将其提供给您的应用程序。
persistence.xml 示例:
这一行是我为 JDBC 资源指定的 JNDI 名称:
您不需要在 persistence.xml 中以这种方式定义与数据库连接相关的任何内容...只需资源的 JNDI 名称
It's better to define a JDBC resource rather than putting the information in the persistence.xml. In this way you are going to take advantage of connection pooling. You are going to define the JNDI name you provided for the JDBC resource in the persistence.xml.
Below I pasted some parts of the Glassfish 3.x help. Check it out. It's really helpful
JDBC connection pool
A JDBC connection pool contains a group of JDBC connections that are created when the connection pool is registered
JDBC resource
A Java DataBase Connectivity (JDBC) resource (data source) provides applications with the means of connecting to a database. Typically, the administrator creates a JDBC resource for each database accessed by the applications deployed in a domain; however, more than one JDBC resource can be created for a database.
Applications get a database connection from a connection pool by looking up a data source on the Java Naming and Directory Interface (JNDI) API tree and then requesting a connection. The connection pool associated with the data source provides the connection to the application.
Think of the data source(JDBC) resource as a factory for a facade of some type of data service(Connection Pool). In this case it implicitly gets a connection from the pool and provides it to your application.
An example persistence.xml:
This line is the JNDI name I gave to my JDBC resourse:
You dont need to define anything related to the database connection in your persistence.xml this way...just the JNDI name of the resource
当您在应用程序服务器中配置数据源时,您只需在
persistence.xml
中设置该数据源的 JNDI 名称即可。When you configure a data source in your application server, all you need to set in
persistence.xml
is the JNDI name of that data source.我在学习 Spring 和 Hibernate 的书中发现了这一点。这本书的名字是Professional Java for Web Applications,作者是Nicholas S. Williams。我认为这对很多人都会有帮助。
Nicholas S. Williams,Web 应用程序的专业 Java,(印第安纳州印第安纳波利斯:John Wiley & Sons, Inc.,2014 年),第 584-585 页
I found this in the book that I read to learn Spring and Hibernate. The book name is Professional Java for Web Applications written by Nicholas S. Williams. I think this will be helpful for many people.
Nicholas S. Williams, Professional Java for Web Applications, (Indianapolis, Indiana: John Wiley & Sons, Inc., 2014), pg 584-585