JBoss 系列五主要目的是演示如何在JBoss 7/WildFly中配置使用Oracle数据库,主要分两大部分Oracle数据库相关的配置,一个使用Oracle数据库的HelloWorld小程序。如下为一简单示意图:
Hello World App部署于JBoss上,Hello World App通过JPA/Hibernate操作数据库,数据库连接是通过JCA/Datasource维护。
我们需要下载,编译生成Hello World App,具体根据系列一中相关步骤,下载,编译:
git clone [email protected]:kylinsoong/csdn.git cd csdn/5 mvn clean install
本部分我们分三块:JBoss 端配置,Oracle数据库端的配置,应用端配置
1. 添加Oracle数据库驱动模块到JBoss
在JBOSS_HOME/modules/目录下创建com/oracle/main目录,将Oracle驱动jari(ojdbc6.jar)拷贝到此目录下,并同在此目录下创建module.xml文件,添加内容如下:
<?xml version="1.0" encoding="UTF-8"?> <module xmlns="urn:jboss:module:1.1" name="com.oracle"> <resources> <resource-root path="ojdbc6.jar"/> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> </dependencies> </module>
编辑JBOSS_HOME/standalone/configuration/standalone.xml文件,在<subsystem xmlns="urn:jboss:domain:datasources处datasources,drivers中添加如下内容:
<driver name="oracle" module="com.oracle"> <xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class> </driver>
使用DBA用户连接到Oracle数据库控制台,创建demo_user/soong,并分配相应的权限,如下:
sqlplus / as sysdba create tablespace TESTSPACE datafile '/oracle/oradata/TESTSPACE.DBF' size 100M create user demo_user identified by soong default tablespace TESTSPACE; grant connect,resource,dba to demo_user;
1. 配置datasource
编辑csdn/5/src/main/resources/JPADemo-ds.xml,修改datasource内容如下所示:
<datasource jndi-name="java:jboss/datasources/JPADemoDS" pool-name="JPADemoPool"> <connection-url>jdbc:oracle:thin:@10.66.192.144:1521:JBOSS</connection-url> <driver>oracle</driver> <security> <user-name>demo_user</user-name> <password>soong</password> </security> </datasource>
2. 配置persistence-unit
编辑csdn/5/src/main/resources/META-INF/persistence.xml,添加persistence-unit内容如下所示:
<persistence-unit name="primary"> <jta-data-source>java:jboss/datasources/JPADemoDS</jta-data-source> <shared-cache-mode>ALL</shared-cache-mode> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.cache.use_second_level_cache" value="true" /> <property name="hibernate.cache.use_query_cache" value="true" /> </properties> </persistence-unit>
1. 部署jpa-helloworld.war到JBoss(参照使用4种方式部署应用到JBoss7/WildFly)。
2. 启动JBoss
[kylin@localhost bin]$ ./standalone.sh
点击Submit会执行数据库查询。
点击Add a new user会执行插入数据库操作。
点击List All Users会列出数据库中所有用户。
4. 结果分析
依次执行上述操作,JBoss后台日志会有如下SQL语句:
12:01:14,482 INFO [stdout] (http-/127.0.0.1:8080-1) Hibernate: select user0_.id as id0_, user0_.firstName as firstName0_, user0_.lastName as lastName0_, user0_.username as username0_ from USERS user0_ where user0_.username=? limit ? 12:01:31,284 INFO [stdout] (http-/127.0.0.1:8080-1) Hibernate: call next value for hibernate_sequence 12:01:31,295 INFO [stdout] (http-/127.0.0.1:8080-1) Hibernate: insert into USERS (firstName, lastName, username, id) values (?, ?, ?, ?) 12:01:45,775 INFO [stdout] (http-/127.0.0.1:8080-1) Hibernate: select user0_.id as id0_, user0_.firstName as firstName0_, user0_.lastName as lastName0_, user0_.username as username0_ from USERS user0_