浏览 641 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-03-27
因为用户需要,需要动态配置连接URL。一边日后连接到他需要的服务器上。
以前看过连接池的配置貌似都是配置中写好URL的。昨天在C3P0上试验了一下,动态设置URL并且已经转换了数据库连接。但是无法获得数据。 而且也有个顾虑: 1、在并发访问的时候。如果一个客户选择新的连接,那么其它访问默认连接的用户会不会受到影响? 2、如此必须重新初始化连接到新的URL,那么性能岂不是也很差? 希望大家帮忙分析一下。谢谢 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
| 返回顶楼 | |
|
时间:2008-03-27
没搞明白你的说法。连接池的配置一般是配置文件和应用服务器配置。你需要连其他服务器停一下应用,改一下配置就可以了。为什么要自己手动改?这种东西谁没事成天改来改去的!
你最好说清楚你的需求。如果客户能随便乱改系统配置,那么你们的需求不正常。如果你们需要连接多个服务器,应该用多数据源或分布式事务处理。 最后回答你的顾虑。如果你能这么做。 在并发访问的时候。如果一个客户选择新的连接,那么其它访问默认连接的用户会不会受到影响? 当然。如果你所谓的新的连接是把原来的数据源修改了。那么所有其他正在处理的用户业务全部都无法继续。 如此必须重新初始化连接到新的URL,那么性能岂不是也很差? 如果你频繁这么做,性能退化到以前老的打开页面,开数据库,处理,关数据库的时代。甚至可能更糟。 |
|
| 返回顶楼 | |
|
时间:2008-03-27
所以还是只能用原始的jdbc实现咯?因为客户需要可以动态配置数据库的,我也是没有办法。。。
所谓动态配置数据库,就是在本地数据库里插入一条数据,包含远程数据库的URL。然后新的功能就可以通过新的URL访问新的数据库了。。。 |
|
| 返回顶楼 | |
|
时间:2008-03-27
说白了就是第一次启动的时候用户可以自己设置数据库,是吧?那么你可以看jdon、jabber等项目。他们第一次安装的时候都可以自己设置数据库信息。
不过这么做其实对你这类项目实在是没什么用。因为jdon、Jabber这些东西都是平台化的东西。安装者的水平参差不齐,为了让大家简化安装才这么设计的。 如果是专门的定制软件,而且是由你们继续维护的。那么这么设计根本就没有任何必要。 你这么设计的话,根本不用弄这个数据库那个数据库的。你用一个配置文件来保存数据库连接信息。第一次启动的时候,先进一个配置页面。配置好了,然后把数据库连接池启动就是了。 不过这么设计,就只能用第三方连接池之类你能自己完全控制的设计。不能用JNDI从应用服务器获得连接池。 最后,应用服务器里也可以很方便的设置数据库连接池。比你自己写网页还方便呢。 |
|
| 返回顶楼 | |
|
时间:2008-03-28
魔力猫咪 写道 说白了就是第一次启动的时候用户可以自己设置数据库,是吧?那么你可以看jdon、jabber等项目。他们第一次安装的时候都可以自己设置数据库信息。
不过这么做其实对你这类项目实在是没什么用。因为jdon、Jabber这些东西都是平台化的东西。安装者的水平参差不齐,为了让大家简化安装才这么设计的。 如果是专门的定制软件,而且是由你们继续维护的。那么这么设计根本就没有任何必要。 你这么设计的话,根本不用弄这个数据库那个数据库的。你用一个配置文件来保存数据库连接信息。第一次启动的时候,先进一个配置页面。配置好了,然后把数据库连接池启动就是了。 不过这么设计,就只能用第三方连接池之类你能自己完全控制的设计。不能用JNDI从应用服务器获得连接池。 最后,应用服务器里也可以很方便的设置数据库连接池。比你自己写网页还方便呢。 ....跑了 |
|
| 返回顶楼 | |
|
时间:2008-03-28
到底是怎么个动态法
随时添加还是怎么 记得以前有个帖子 讨论过每个用户连接不同的DB的问题 你可以搜索一下 |
|
| 返回顶楼 | |
|
时间:2008-03-29
动态建立JDBC连接的话可以用连接池实现吗?
---------------- 大多数情况下,用户只需要链接到一个数据库, 数据库URL连接只有一个. 因此,连接池提供了配置文件的方式,让用户配置一个唯一的数据库连接池. 这只是一种简单用法. 既然连接池提供了配置文件的方式设置数据库连接URL,必然也会提供API让程序员用代码设置数据库连接URL. 如果要同时使用多个数据库的连接池,或者动态更换数据库,就需要直接使用连接池的API,而不是配置文件. 比如, 常用的Java数据库连接池 DBCP 的例子 dbcp 主站 http://commons.apache.org/dbcp/ dbcp API 建立数据库连接池的例子. http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/ManualPoolingDataSourceExample.java?view=markup
//
// Here's a simple example of how to use the PoolingDataSource.
// In this example, we'll construct the PoolingDataSource manually,
// just to show how the pieces fit together, but you could also
// configure it using an external conifguration file in
// JOCL format (and eventually Digester).
//
//
// Note that this example is very similiar to the PoolingDriver
// example. In fact, you could use the same pool in both a
// PoolingDriver and a PoolingDataSource
//
//
// To compile this example, you'll want:
// * commons-poo-1.3.jar
// * commons-dbcp-1.2.2.jar
// * j2ee.jar (for the javax.sql classes)
// in your classpath.
//
// To run this example, you'll want:
// * commons-poo-1.3.jar
// * commons-dbcp-1.2.2.jar
// * j2ee.jar (for the javax.sql classes)
// * the classes for your (underlying) JDBC driver
// in your classpath.
//
// Invoke the class using two arguments:
// * the connect string for your underlying JDBC driver
// * the query you'd like to execute
// You'll also want to ensure your underlying JDBC driver
// is registered. You can use the "jdbc.drivers"
// property to do this.
//
// For example:
// java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver \
// -classpath commons-pool-1.3.jar:commons-dbcp-1.2.2.jar:j2ee.jar:oracle-jdbc.jar:. \
// ManualPoolingDataSourceExample
// "jdbc:oracle:thin:scott/tiger@myhost:1521:mysid"
// "SELECT * FROM DUAL"
//
public class ManualPoolingDataSourceExample {
public static void main(String[] args) {
//
// First we load the underlying JDBC driver.
// You need this if you don't use the jdbc.drivers
// system property.
//
System.out.println("Loading underlying JDBC driver.");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Done.");
//
// Then, we set up the PoolingDataSource.
// Normally this would be handled auto-magically by
// an external configuration, but in this example we'll
// do it manually.
//
System.out.println("Setting up data source.");
DataSource dataSource = setupDataSource(args[0]);
System.out.println("Done.");
//
// Now, we can use JDBC DataSource as we normally would.
//
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
System.out.println("Creating connection.");
conn = dataSource.getConnection();
System.out.println("Creating statement.");
stmt = conn.createStatement();
System.out.println("Executing statement.");
rset = stmt.executeQuery(args[1]);
System.out.println("Results:");
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()) {
for(int i=1;i<=numcols;i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
try { rset.close(); } catch(Exception e) { }
try { stmt.close(); } catch(Exception e) { }
try { conn.close(); } catch(Exception e) { }
}
}
public static DataSource setupDataSource(String connectURI) {
//
// First, we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
ObjectPool connectionPool = new GenericObjectPool(null);
//
// Next, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
//
// Now we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
//
// Finally, we create the PoolingDriver itself,
// passing in the object pool we created.
//
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
return dataSource;
}
}
|
|
| 返回顶楼 | |
|
时间:2008-03-30
不用那么麻烦,直接调用相关数据链接池的配置就行了,例如DBCP是BasicDataSource,C3P0的忘记了,应该都用的,然后在外面稍微包一层就可以了。
|
|
| 返回顶楼 | |










