那在getConnectionRo里,为什么有// Get new呢,我还不是很理解代码的意思,你能在代码里,每行都给我加注释啊,那样我会更明白一些 getConnectionRo调用的createConnections 的代码如下 private static Connection[] createConnections (int trxLevel) { s_log.debug("createConnections (" + s_conCacheSize + ") " + s_cc.getConnectionURL() + ", UserID=" + s_cc.getDbUid() + ", TrxLevel=" + trxLevel); Connection cons[] = new Connection[s_conCacheSize]; try { for (int i = 0; i < s_conCacheSize; i++) { cons[i] = s_cc.getConnection (true, trxLevel); // auto commit if (cons[i] == null) System.err.println("createConnections - connection is NULL"); // don't use log } } catch (Exception e) { // Don't use Log System.err.println("DB.createConnections - " + e.getMessage()); } return cons; } // createConnections
private static Connection[] createConnections (int trxLevel) { s_log.debug("createConnections (" + s_conCacheSize + ") " + s_cc.getConnectionURL() + ", UserID=" + s_cc.getDbUid() + ", TrxLevel=" + trxLevel);//写日志 Connection cons[] = new Connection[s_conCacheSize];//新建连接,可以保证不会超过最大的连接数! try { for (int i = 0; i < s_conCacheSize; i++) { cons[i] = s_cc.getConnection (true, trxLevel); // auto commit,好像是要得到连接的同时,也要求level等级,是不是有级别的限定! if (cons[i] == null) System.err.println("createConnections - connection is NULL"); // don't use log } } catch (Exception e) { // Don't use Log System.err.println("DB.createConnections - " + e.getMessage()); } return cons; } // createConnections
cache基本上是用容器类实现的,包括:set list map 刚开始就建立好一些连接,然后就把这些建立好的连接,也就是对象放到上面的那些容器里 以后要是用的话,基本上就是从容器里取出对象,而不是重新建立连接,因为这样比较费资源。连接池和cache的原理就是这样。至于选择哪个容器,要看具体情况,包括经常做哪些操作等。因为上面容器的数据结构都是不一样的。
private static int s_conCacheSize = 2; 还有能把这段代码的作用给我讲一下吗,最好在代码中给我加注释,呵呵
这段代码好像是建立连接用的!
setDBTarget连接目标数据库!
getConnectionRW 得到可读写的数据库!
Connection connection = s_connections[pos % s_conCacheSize];来保证连接不会超出范围!
getConnectionRo调用的createConnections 的代码如下
private static Connection[] createConnections (int trxLevel)
{
s_log.debug("createConnections (" + s_conCacheSize + ") " + s_cc.getConnectionURL()
+ ", UserID=" + s_cc.getDbUid() + ", TrxLevel=" + trxLevel);
Connection cons[] = new Connection[s_conCacheSize];
try
{
for (int i = 0; i < s_conCacheSize; i++)
{
cons[i] = s_cc.getConnection (true, trxLevel); // auto commit
if (cons[i] == null)
System.err.println("createConnections - connection is NULL"); // don't use log
}
}
catch (Exception e)
{
// Don't use Log
System.err.println("DB.createConnections - " + e.getMessage());
}
return cons;
} // createConnections
{
s_log.debug("createConnections (" + s_conCacheSize + ") " + s_cc.getConnectionURL()
+ ", UserID=" + s_cc.getDbUid() + ", TrxLevel=" + trxLevel);//写日志
Connection cons[] = new Connection[s_conCacheSize];//新建连接,可以保证不会超过最大的连接数!
try
{
for (int i = 0; i < s_conCacheSize; i++)
{
cons[i] = s_cc.getConnection (true, trxLevel); // auto commit,好像是要得到连接的同时,也要求level等级,是不是有级别的限定!
if (cons[i] == null)
System.err.println("createConnections - connection is NULL"); // don't use log
}
}
catch (Exception e)
{
// Don't use Log
System.err.println("DB.createConnections - " + e.getMessage());
}
return cons;
} // createConnections
刚开始就建立好一些连接,然后就把这些建立好的连接,也就是对象放到上面的那些容器里
以后要是用的话,基本上就是从容器里取出对象,而不是重新建立连接,因为这样比较费资源。连接池和cache的原理就是这样。至于选择哪个容器,要看具体情况,包括经常做哪些操作等。因为上面容器的数据结构都是不一样的。