简体   繁体   中英

JSTL persistent database connections

Are the database connections used by JSTL <sql:query> tag persistent?

  1. I mean if I use a lot of <sql:query> tags on a page then will they share the same database connection?
  2. What about across the page connections? Does JSTL provide a way to use persistent connections like mysql_pconnect() in PHP?

In JSTL for SQL tags you can give the datasource which you want to use for DB connections. It is upto the datasource which decides the management of DB connections . A SQL tag gives the datasource information like this:

<sql:query var="order" 
  dataSource="${applicationScope.orderDS}">
   select * from PUBLIC.orders where id = ?
   <sql:param value="${id}" />
</sql:query>

The datasource attribute value can be of the following types:

  1. Datasource - in which case this datasource will be used.
  2. String - in which case JDNI lookup will be done with that name.
  3. NULL - in which case it will use the datasource set by tag.

In case you have a SQL query tags inside transaction tag . All the queries inside the transaction share the same connection for transaction management.

<sql:transaction>
   <sql:query var="order" 
      dataSource="${applicationScope.orderDS}">
         select * from PUBLIC.orders where id = ?
         <sql:param value="${id}" />
   </sql:query>
   <sql:update var="order" 
      sql="update PUBLIC.orders set book_name = ? where id = ?">
         <sql:param value="${name}" />
         <sql:param value="${id}" />
   </sql:query>
<sql:transaction>

Across multiple pages, you are better off having a backing servlet with session scope to hold your JPA session. But as stated previously, its really bad practice to put SQL in your view. Probably you should look at your design again, doesnt sound correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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