用了 Derby 数据库好一段时间了,雁过留痕。
作为内存数据库或嵌入式数据库,Derby 是一个很好的选择,完全由 Java 实现,在 JDK1.6 中 Derby 已经作为其一部分来发布了,称之为 JavaDB。
Derby 数据库通过 derby.properties 文件来配置,这个文件需要放在程序的 classpath 或者 jdk 的 javadb classpath 路径中。下面是一些配置的例子:
---------------------------- Derby derby.properties information ----------------------------
--### Derby System Home Directory
derby.system.home=D:/workspace/Boulevard/BoulevardDB
--### Derby Authentication Configuration and Turn On
derby.connection.requireAuthentication=true
derby.authentication.provider=BUILTIN
derby.database.propertiesOnly=true
--### User/Password Definition (System Level)
derby.user.alfred=alfred
--### User Authorization Definition (System Level)
derby.database.defaultAccessMode=fullAccess
derby.fullAccessUsers=cube
--### Some Others Configuration
derby.infolog.append=true
derby.storage.pageSize=4096
derby.storage.pageReservedSpace=60
derby.locks.deadlockTimeout=20
derby.locks.waitTimeout=30
---------------------------- Derby derby.properties information ----------------------------
Derby 数据库可以启动为两种不同的模式:嵌入式模式 或 服务器模式
1. 嵌入式模式(embedded):当 Java 应用程序第一次尝试连接数据库时启动数据库实例,当程序进程结束时数据库实例也被关闭。当数据库实例启动后,仅仅启动它的进程可以访问,其它任何外部进程(运行在当前进程的JVM之外的程序)都不能访问。
下面的命令用于启动并连接一个嵌入式数据库实例:
------------------------------- Derby connection information -------------------------------
-- ### Use ij command to connect embeded database; if not exist, create it.
CONNECT 'jdbc:derby:data;create=true;user=alfred;password=alfred' AS CUBE;
-- ### Use ij command to connect embeded database; if not exist, don't create it, throw out error.
CONNECT 'jdbc:derby:data;user=alfred;password=alfred' AS CUBE;
------------------------------- Derby connection information -------------------------------
2. 服务器模式(network server):数据库实例只能由命令行启动:"...\javadb\bin\startNetworkServer.bat",该实例始终有效,直到通过命令行停止:"...\javadb\bin\stopNetworkServer.bat"。任何运行在本地或远程的JVM进程都可以访问,不会随着访问它的程序的结束而关闭。
下面的命令用于连接(不能启动)服务器数据库实例:
------------------------------- Derby connection information -------------------------------
-- ### Use ij command to connect network server database, reading default repository;
-- ### If not exist, create it.
CONNECT 'jdbc:derby://localhost:1527/data;create=true;user=alfred;password=alfred' AS BOULEVARD;
-- ### Use ij command to connect network server database; reading default repository;
-- ### If not exist, don't create it, throw out error.
CONNECT 'jdbc:derby://localhost:1527/data;user=alfred;password=alfred' AS BOULEVARD;
-- ### Use ij command to connect network server database, reading specific repository;
-- ### If not exist, create it.
CONNECT 'jdbc:derby://localhost:1527/D:/workspace/Boulevard/BoulevardDB/data;create=true;user=alfred;password=alfred' AS BOULEVARD;
-- ### Use ij command to connect network server database, reading specific repository;
-- ### If not exist, don't create it, throw out error.
CONNECT 'jdbc:derby://localhost:1527/D:/workspace/Boulevard/BoulevardDB/data;user=alfred;password=alfred' AS BOULEVARD;
------------------------------- Derby connection information -------------------------------