简体   繁体   English

数据库连接池和数据库连接?

[英]Database connection pool and database connection?

I have created a desktop application and I have connect it to a MySQL database with a database connection (bean/class) and I can CRUD. 我创建了一个桌面应用程序,并通过数据库连接(bean / class)将其连接到MySQL数据库,并且可以CRUD。 I have seen on the NetBeans site that they create a connection pool on a web application. 我在NetBeans站点上看到他们在Web应用程序上创建了一个连接池。

Is a connection pool the same with the class/bean on a desktop application? 连接池与桌面应用程序上的类/ bean是否相同?

Does this mean that i create a bean/class like a desktop application that is connected with to DB model(MVC), or do i have to do something else? 这是否意味着我创建了一个与桌面应用程序连接到数据库模型(MVC)的bean /类,还是必须做其他事情?

On a Glassfish server you do the connection pool with a wizard; 在Glassfish服务器上,使用向导创建连接池。 on Apache you do not. 在Apache上您不需要。 Do I have to create the DB connection bean for Apache? 我必须为Apache创建数据库连接bean吗?

What are the practices (beans, something else?) to connect a DB to a web application? 将数据库连接到Web应用程序有哪些实践(bean,还有其他方法)?

I have also read about Hibernate, but I don't understand the use of it. 我还阅读了有关Hibernate的信息,但我不了解它的用法。 Where can hibernate help? 冬眠在哪里可以提供帮助? I mean, it's ORM, but what can Hibernate do for me so that my code is easier? 我的意思是,它是ORM,但是Hibernate可以为我做什么,以便使我的代码更容易? I think I'm missing the point of ORM 我想我错过了ORM的要点

Hibernate will help you with your transaction management. Hibernate将帮助您进行事务管理。 It will enable you to open several different connections to the database, and also give you warnings when you are using unavailable objects (like beans that gets pulled in from different threads). 这将使您能够打开到数据库的多个不同连接,并在使用不可用的对象(例如从不同线程拉入的bean)时向您发出警告。

A concrete example of where Hibernate's ORM will make your code easier is when you are querying the DB. 当查询数据库时,Hibernate的ORM将使您的代码更容易使用的一个具体示例。 Instead of writing the standard SQL queries as strings, you can use Criteria -queries. 您可以使用Criteria -queries,而不是将标准SQL查询编写为字符串。

In Java, DB connections always use a JDBC driver. 在Java中,数据库连接始终使用JDBC驱动程序。 No DB that I know of allows to run more than a single SQL command over a single connection at the same time , so each connection becomes bottleneck if your application can run several SQL commands at the same time (the usual case for web servers where hundreds of users can interact with the database at the same time). 据我所知,没有一个数据库允许在一个连接上同时运行多个SQL命令,因此,如果您的应用程序可以同时运行多个SQL命令,则每个连接都会成为瓶颈(Web服务器通常会出现数百个这种情况)的用户可以同时与数据库进行交互)。

UPDATE : What I'm saying is: You can easily daisy-chain commands over a single connection (like UPDATE ... ; COMMIT ) but you can't send two UPDATE commands at the same time -- you always have to wait for the first command to complete before you can send the next. UPDATE :我的意思是:您可以通过单个连接轻松地菊花链式命令(例如UPDATE ... ; COMMIT ),但不能同时发送两个UPDATE命令-您总是必须等待在发送下一个命令之前,请先完成第一个命令。 Some databases allow to send several commands in a single query but they are executed one after the other and not all at the same time . 一些数据库允许在单个查询中发送多个命令,但是它们一个接一个地执行而不是同时执行 Think about it: If you could run several commands concurrently over a single connection, how would you know in which order they were executed? 想一想:如果您可以在单个连接上同时运行多个命令,您怎么知道它们以什么顺序执行?

On top of that, creating DB connections is expensive for most DBs. 最重要的是,对于大多数数据库而言,创建数据库连接非常昂贵。 Hence they are created in advance during application startup and held in a pool. 因此,它们是在应用程序启动期间预先创建的,并保存在一个池中。 As soon as you "connect" to the database with the pooled JDBC driver, it picks an unused connection from the pool and returns it. 一旦使用池化的JDBC驱动程序“连接”到数据库,它就会从池中选择一个未使用的连接并返回它。 That (almost) takes no time. 那(几乎)不需要时间。 When you "close" the connection, it's returned to the pool. 当您“关闭”连接时,它将返回到池中。

As an additional benefit, the pool can keep the connections alive. 另一个好处是,池可以使连接保持活动状态。 So you never need to worry about connection errors when you need a new connection (well, as long as the DB is running). 因此,当您需要新的连接时(只要数据库正在运行),您就不必担心连接错误。

From the application side, this is either transparent (most JDBC drivers either pool internally today or they have a pooling API). 从应用程序方面看,这是透明的(大多数JDBC驱动程序现在在内部进行池化,或者具有池化API)。 If your JDBC driver doesn't, you can always use a pool like DBCP. 如果您的JDBC驱动程序没有,则可以始终使用DBCP之类的池。 The pool handles all the nasty details and you write your application against the pool API instead of using JDBC directly. 池处理所有令人讨厌的细节,您针对池API编写应用程序,而不是直接使用JDBC。 The docs will tell you how to do it. 该文档将告诉您如何操作。

How Hibernate is a different beast. 冬眠如何与众不同。 Hibernate is a layer on top of JDBC that can transform POJOs into SQL and back. Hibernate是JDBC之上的一层,可以将POJO转换为SQL,然后转换回SQL。

So instead of saying INSERT INTO data(ID, VALUE) values (?, ?) , you can say 因此,您无需说INSERT INTO data(ID, VALUE) values (?, ?)

class Pojo { long id; String value; }
Pojo demo = new Pojo();
demo.value = "Test";

session.persist(demo);

and Hibernate will create the SQL for you and send it to the DB. 然后Hibernate将为您创建SQL并将其发送到数据库。 At this stage, it doesn't make your life easier. 在现阶段,这并不能使您的生活更轻松。 Hibernate starts to shine when you change your Pojos: 更改Pojos时,Hibernate开始发光:

class Pojo { long id; String value; 
    String name; // Oops ... forget the name
}
Pojo demo = new Pojo();
demo.name = "John";
demo.value = "Test";

session.persist(demo);

Hibernate will change the DB definition accordingly and update all the SQL commands it needs to load and save the objects. Hibernate将相应地更改数据库定义,并更新它加载和保存对象所需的所有SQL命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM