开发者

Database connection pool and database connection?

开发者 https://www.devze.com 2023-02-13 21:38 出处:网络
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.I ha开发者_开发百科ve seen on the NetBeans site that they create a

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. I ha开发者_开发百科ve seen on the NetBeans site that they create a connection pool on a web application.

Is a connection pool the same with the class/bean on a desktop application?

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?

On a Glassfish server you do the connection pool with a wizard; on Apache you do not. Do I have to create the DB connection bean for Apache?

What are the practices (beans, something else?) to connect a DB to a web application?

I have also read about Hibernate, but I don't understand the use of it. Where can hibernate help? I mean, it's ORM, but what can Hibernate do for me so that my code is easier? I think I'm missing the point of ORM


Hibernate will help you with your transaction management. 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).

A concrete example of where Hibernate's ORM will make your code easier is when you are querying the DB. Instead of writing the standard SQL queries as strings, you can use Criteria-queries.


In Java, DB connections always use a JDBC driver. 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).

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. 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. 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). If your JDBC driver doesn't, you can always use a pool like DBCP. The pool handles all the nasty details and you write your application against the pool API instead of using JDBC directly. 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.

So instead of saying INSERT INTO data(ID, VALUE) values (?, ?), you can say

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. At this stage, it doesn't make your life easier. Hibernate starts to shine when you change your Pojos:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消