Topic: Struts 数据源配置和使用 |
Print this page |
1.Struts 数据源配置和使用 | Copy to clipboard |
Posted by: glistar Posted on: 2003-03-17 11:12 (1)。单数据源配置 如果系统中只有一个数据源的情况下: 以下以 PostgreSQL 为例,配置文件如下, <data-sources> <data-source> <set-property property="type" value="javax.sql.DataSource" /> <set-property property="driverClass" value="org.postgresql.Driver" /> <set-property property="url" value="jdbc:postgresql://DBServer:5432/DBName" /> <set-property property="maxCount" value="20" /> <set-property property="minCount" value="2" /> <set-property property="user" value="userName" /> <set-property property="password" value="userPassword" /> </data-source> </data-sources> 在Action中调用: DataSource ds = this.getDataSource(request); 这样就可以取得以上配置的数据源,然后再取连接。 注意: A.上述的数据源取得方法只适用于配置了单个数据源。 B.连接用后一定要调用 close()方法,以将连接返回连接池中,已备重用。 (2)。多数据源配置 多数据源配置中,在 Easy-Struts 和其他的图形化配置工具中,目前都有问题,主要是 Struts 配置文件的 DTD 已经改过。 以下再提供 MySQL 数据源和 PostgreSQL 数据源为例,参见配置如下: <data-sources> <data-source> <set-property property="key" value="postDS" /> <set-property property="type" value="javax.sql.DataSource" /> <set-property property="driverClass" value="org.postgresql.Driver" /> <set-property property="url" value="jdbc:postgresql://DBServer:5432/DBName" /> <set-property property="maxCount" value="20" /> <set-property property="minCount" value="2" /> <set-property property="user" value="userName" /> <set-property property="password" value="userPassword" /> </data-source> <data-source> <set-property property="key" value="mysqlDS" /> <set-property property="type" value="javax.sql.DataSource" /> <set-property property="driverClass" value="com.mysql.jdbc.Driver" /> <set-property property="url" value="jdbc:mysql://DBServer:3306/DBName" /> <set-property property="maxCount" value="20" /> <set-property property="minCount" value="2" /> <set-property property="user" value="userName" /> <set-property property="password" value="userPassword" /> </data-source> </data-sources> 在Action中调用: //MySQL 数据源取得 DataSource mysqlds = this.getDataSource(request,"mysqlDS"); //PostgreSQL 数据源取得 DataSource postds = this.getDataSource(request,"postDS"); 连接的使用和注意事项同(1)。 有些书籍中不建议在 Action 中直接调用数据源,我觉得未尝不可。 本人已经做过压力测试,在Action中进行对数据库的频频操作, 当有 500 用户在线压力 30 分钟,未见任何异常。 测试, 如有遗漏,请多指教。 |
2.Re:Struts 数据源配置和使用 [Re: glistar] | Copy to clipboard |
Posted by: dapan Posted on: 2003-03-17 12:35 不错! 不过我一般自己做DBSource。 ^_^ |
3.Re:Struts 数据源配置和使用 [Re: glistar] | Copy to clipboard |
Posted by: floater Posted on: 2003-03-19 03:39 glistar wrote: I personally think the load is not the reason not use db source in action, but the seperation of them is the reason. |
4.Re:Struts 数据源配置和使用 [Re: glistar] | Copy to clipboard |
Posted by: glistar Posted on: 2003-03-19 09:13 There exists a separation between view and model,why separate the datasource from action? Struts developers hope we gain datasource from actions,you can read source codes! |
5.Re:Struts 数据源配置和使用 [Re: glistar] | Copy to clipboard |
Posted by: arjooe Posted on: 2003-03-21 15:56 在我的Web Service中,我没有使用Struts中Action提供的getDataSource(request)方法来取得Connection, 我利用Jakarta.Commons的DBCP包写了一个Plugin,在ActionServlet初始化时将此连接池对象存在ServletContext中, 以利用它的Pooling Connection特性. 在Action中, 我们使用该对象的getConnection()方法来操作数据库. 我不知道Struts的DataSource本身是否有Connection Pool之功能, 请大家指教. 代码如下: package bss.plugin; /** * * @author pms24133 */ import java.util.Iterator; import java.util.Vector; import javax.sql.DataSource; import javax.servlet.ServletContext; import org.apache.struts.action.Action; import org.apache.struts.action.PlugIn; import org.apache.struts.config.ApplicationConfig; import org.apache.struts.action.ActionServlet; import org.apache.struts.config.DataSourceConfig; import org.apache.commons.pool.ObjectPool; import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.commons.dbcp.ConnectionFactory; import org.apache.commons.dbcp.DataSourceConnectionFactory; import org.apache.commons.dbcp.PoolableConnectionFactory; import org.apache.commons.dbcp.PoolingDataSource; public class DBCPplugin implements PlugIn { private ActionServlet servlet = null; private Vector dbcpvtr = new Vector(); private String[] dskey = null; public void destroy(){ // Remove all physical connections represented by datasource stored in ServletContex. Iterator itr = dbcpvtr.iterator(); while(itr.hasNext()){ try{ GenericObjectPool gop = (GenericObjectPool)itr.next(); gop.close(); gop = null; }catch(Exception e){ servlet.log("System errors::::" + e); }finally{ } } this.dbcpvtr = null; for(int i =0; i<dskey.length; i++){ PoolingDataSource pds = (PoolingDataSource) (servlet.getServletContext(). getAttribute(dskey[i])); pds = null; } dskey = null; } public void init(ActionServlet servlet,ApplicationConfig config){ // Data Sources configed in /WEB-INF/struts-config.xml, // and can be retrieved through config.findDatasourceConfigs() in DataSourceConfig object format; // We assign data from these configs to DBCP config. this.servlet = servlet; DataSourceConfig dscs[] = config.findDataSourceConfigs(); if (dscs == null) { dscs = new DataSourceConfig[0]; servlet.log("No DataSource configed yet."); } this.dskey = new String[dscs.length]; // Process each DataSource config according key. for( int i = 0; i < dscs.length; i++){ // Retrieve each DataSource from ActionServlet Context, DataSource ds = (DataSource) servlet.getServletConfig().getServletContext().getAttribute( dscs[i].getKey() ); // Define a connection Pool for this Data Source definition, //ObjectPool connectionPool = new GenericObjectPool(connectionFactory); ObjectPool connectionPool = new GenericObjectPool(null); // Construct a Conneciton Factory for this Data Source, ConnectionFactory connectionFactory = new DataSourceConnectionFactory(ds); // Construct connectionpool with connection factory try{ PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); /* * Parameter definition of constructor method of PoolableConnectionFactory * * connectionFactory - the ConnectionFactory from which to obtain base Connections * connectionPool - the ObjectPool in which to pool those Connections * stmtPoolFactory - the KeyedObjectPoolFactory to use to create KeyedObjectPools for pooling PreparedStatements, or null to disable PreparedStatement pooling * validationQuery - a query to use to validate Connections. Should return at least one row. May be null * defaultReadOnly - the default "read only" setting for borrowed Connections * defaultAutoCommit - the default "auto commit" setting for returned Connections */ PoolingDataSource dataSource = new PoolingDataSource(connectionPool); // Store connectionpool to Application Context for further globle use; servlet.getServletContext().setAttribute( dscs[i].getKey(),dataSource); dbcpvtr.add(connectionPool); servlet.log("Store org.apache.commons.dbcp.PoolingDataSource with key '"+ dscs[i].getKey() + "' to Application Context."); }catch(Exception e){ servlet.log("Initiating Datasource error: " + e.toString() ); } } }//End of init method }//End of class |
6.Re:Struts 数据源配置和使用 [Re: arjooe] | Copy to clipboard |
Posted by: glistar Posted on: 2003-03-21 16:02 Struts 利用 commons-dbcp.jar 包主要就是完成连接池功能的. 因此你不必自己写连接功能,而是直接按照第一贴的内容配置和使用就可以! |
7.Re:Struts 数据源配置和使用 [Re: glistar] | Copy to clipboard |
Posted by: arjooe Posted on: 2003-03-21 17:21 谢谢! |
8.Re:Struts 数据源配置和使用 [Re: glistar] | Copy to clipboard |
Posted by: floater Posted on: 2003-03-25 01:13 glistar wrote: action is in C, db is in M. That's why I diff. I loaded their code into jb and read them last year. My impression with their source code was they started with pre-mature code, and have to carry them all the way along. |
9.Re:Struts 数据源配置和使用 [Re: glistar] | Copy to clipboard |
Posted by: lihongtao Posted on: 2003-03-25 09:04 我也不赞成在Action里面写业务逻辑 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |