日期:2014-05-16  浏览次数:20522 次

openfire(3)数据库浅析

openfire 中的连接有几种,可以自己实现接口ConnectionProvider,修改openfire.xml中的connectionProvider的属性为自己实现的类。

<connectionProvider>
??? <className>org.jivesoftware.database.DefaultConnectionProvider</className>
? </connectionProvider>
? <database>
??? <defaultProvider>
????? <driver>com.mysql.jdbc.Driver</driver>
????? <serverURL>jdbc:mysql://localhost:3306/openfire?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;zeroDateTimeBehavior=convertToNull</serverURL>
????? <username>root</username>
????? <password>admin</password>
????? <testSQL>select 1</testSQL>
????? <testBeforeUse>false</testBeforeUse>
????? <testAfterUse>false</testAfterUse>
????? <minConnections>5</minConnections>
????? <maxConnections>25</maxConnections>
????? <connectionTimeout>1.0</connectionTimeout>
??? </defaultProvider>
? </database>

?

openfire实现了3个相关的接口,JNDIDataSourceProvider,EmbeddedConnectionProvider,DefaultConnectionProvider(默认的连接管理器),下面将分别解析这三个类。

?

?

1. DefaultConnectionProvider:是openfire提供的默认连接管理器,使用了org.logicalcobwebs.proxool.ProxoolDriver提供的连接池,

其中ConnectionProvider的start方法如下,主要设置连接池的相关参数

?

?public void start() {
??????? proxoolURL = "proxool.openfire:"+getDriver()+":"+getServerURL();
??????? settings = new Properties();
??????? settings.setProperty("proxool.maximum-activetime", Integer.toString(activeTimeout));
??????? settings.setProperty("proxool.maximum-connection-count", Integer.toString(getMaxConnections()));
??????? settings.setProperty("proxool.minimum-connection-count", Integer.toString(getMinConnections()));
??????? settings.setProperty("proxool.maximum-connection-lifetime", Integer.toString((int)(86400000 * getConnectionTimeout())));
??????? settings.setProperty("proxool.test-before-use", testBeforeUse.toString());
??????? settings.setProperty("proxool.test-after-use", testAfterUse.toString());
??????? settings.setProperty("proxool.house-keeping-test-sql", testSQL);
??????? settings.setProperty("user", getUsername());
??????? settings.setProperty("password", (getPassword() != null ? getPassword() : ""));
??? }

?

2.EmbeddedConnectionProvider(嵌入式连接管理器),主要是用于openfire中采用的hsqldb.

? public Connection getConnection() throws SQLException {
??????? try {
??????????? Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
??????????? return DriverManager.getConnection(proxoolURL, settings);
??????? }
??????? catch (ClassNotFoundException e) {
??????????? throw new SQLException("EmbeddedConnectionProvider: Unable to find driver: "+e);
??????? }
??? }

,此也采用org.logicalcobwebs.proxool.ProxoolDriver代理连接池。

?

hsqldb,的存放地址为openfire目录下的embedded-db中。

? File databaseDir = new File(JiveGlobals.getHomeDirectory(), File.separator + "embedded-db");
??????? // If the database doesn't exist, create it.
??????? if (!databaseDir.exists()) {
??????????? databaseDir.mkdirs();
??????? }

??????? try {
??????????? serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() + File.separator + "openfire";
??????? }
??????? catch (IOException ioe) {
??????????? Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe);
??????? }

?

Hsqldb是一个开放源代码的JAVA数据库,其具有标准的SQL语法和JAVA接口,它可以自由使用和分发,非常简洁和快速的。具有Server模式,进程内模式(In-Process)和内存模式(Memory-Only)三种。运行Hsqldb需要hsqldb.jar包, 它包含了一些组件和程序。每个程序需要不同的命令来运行。

更多关于Hsqldb请查看其它相关资料,在openfire中使用的标准的sql。

?

?

3.JNDIDataSourceProvider

openfire提供的JNDI连接器。

?public void start() {
??????? if (dataSourceName == null || dataSourceName.equals("")) {
???????????