Linux下JBoss到MySql的数据源配置
一、安装jdk并设置好JAVA_HOME,CLASSPATH等环境变量二、给JBoss配置MySql数据源下面的JBOSS_HOME是指JBoss的安装路径,比如/usr/local/jboss1、把MySql的驱动拷到JBoss的$JBOSS_HOME/server/default/lib目录下,并把它加入到CLASSPATH环境变量中。 比如MySql驱动是mysql-connector-
·
一、安装jdk并设置好JAVA_HOME,CLASSPATH等环境变量
二、给JBoss配置MySql数据源
下面的JBOSS_HOME是指JBoss的安装路径,比如/usr/local/jboss
1、把MySql的驱动拷到JBoss的$JBOSS_HOME/server/default/lib目录下,并把它加入到CLASSPATH环境变量中。
比如MySql驱动是mysql-connector-java-5.0.3-bin.jar那么把下面一行加入到~/.bash_profile
文件中:
CLASSPATH=$CLASSPATH:/JBOSS_HOME/server/default/lib/mysql-connector-java-5.0.3-bin.jar
2、把$JBOSS_HOME/docs/examples/jca/mysql-ds.xml文件拷到$JBOSS_HOME/server/default/deploy目录下,
然后把内容修改成如下内容:
注意,用户名和密码要改成你实际的用户名和密码,localhost可以改成实际的主机名。
这个时候JBoss只能够使用8080端口,把开
$JBOSS_HOME/server/default/deploy/jbossweb-tomcat55.sar/server.xml
文件,找到如下内容:
把port="8080"改成port="80",这样JBoss就可以使用80端口号了
三、测试例子
打开mysql,使用test数据库,创建一个表mytable,并插入几条记录,命令如下:
CREATE TABLE mytable (name VARCHAR(20), sex CHAR(1), addr VARCHAR(30));
insert into mytable(name, sex, addr) values("plusboy", 'M', "Guangzhou");
insert into mytable(name, sex, addr) values("YanDingcheng", 'M', "Guangzhou");
编写一个myTest.jsp程序用于测试,把该文件放到目录
$JBOSS_HOME/server/default/deploy/jbossweb-tomcat55.sar/ROOT.war
下面。
文件名:myTest.jsp
重启JBoss和MySql。
在浏览器地址栏输入http://localhost/myTest.jsp
哈哈,数据库操作成功。
四、解决MySql重启后JBoss重连问题
jboss的连接池支持根据SQLException的类型来确定是应用的异常还是不可恢复的数据库连接的异常,因此可以在
datasource 的配置里加上
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
这样就不需要每次获取连接的时候都执行一次查询了,性能提高了很多,
只有当发生SQLException并且他侦测到这个异常导致连接无法使用时才会丢弃这个连接
二、给JBoss配置MySql数据源
下面的JBOSS_HOME是指JBoss的安装路径,比如/usr/local/jboss
1、把MySql的驱动拷到JBoss的$JBOSS_HOME/server/default/lib目录下,并把它加入到CLASSPATH环境变量中。
比如MySql驱动是mysql-connector-java-5.0.3-bin.jar那么把下面一行加入到~/.bash_profile
文件中:
CLASSPATH=$CLASSPATH:/JBOSS_HOME/server/default/lib/mysql-connector-java-5.0.3-bin.jar
2、把$JBOSS_HOME/docs/examples/jca/mysql-ds.xml文件拷到$JBOSS_HOME/server/default/deploy目录下,
然后把内容修改成如下内容:
<?
xml version="1.0" encoding="UTF-8"
?>
< datasources >
< local-tx-datasource >
< jndi-name > MySqlDS </ jndi-name >
< connection-url > jdbc:mysql://localhost:3306/test </ connection-url >
< driver-class > com.mysql.jdbc.Driver </ driver-class >
< user-name > root </ user-name >
< password > 123456 </ password >
< exception-sorter-class-name > org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </ exception-sorter-class-name >
< metadata >
< type-mapping > mySQL </ type-mapping >
</ metadata >
</ local-tx-datasource >
</ datasources >
< datasources >
< local-tx-datasource >
< jndi-name > MySqlDS </ jndi-name >
< connection-url > jdbc:mysql://localhost:3306/test </ connection-url >
< driver-class > com.mysql.jdbc.Driver </ driver-class >
< user-name > root </ user-name >
< password > 123456 </ password >
< exception-sorter-class-name > org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </ exception-sorter-class-name >
< metadata >
< type-mapping > mySQL </ type-mapping >
</ metadata >
</ local-tx-datasource >
</ datasources >
注意,用户名和密码要改成你实际的用户名和密码,localhost可以改成实际的主机名。
这个时候JBoss只能够使用8080端口,把开
$JBOSS_HOME/server/default/deploy/jbossweb-tomcat55.sar/server.xml
文件,找到如下内容:
<!--
A HTTP/1.1 Connector on port 8080
-->
< Connector port ="8080" address ="${jboss.bind.address}"
maxThreads ="250" strategy ="ms" maxHttpHeaderSize ="8192"
emptySessionPath ="true"
enableLookups ="false" redirectPort ="8443" acceptCount ="100"
connectionTimeout ="20000" disableUploadTimeout ="true" />
< Connector port ="8080" address ="${jboss.bind.address}"
maxThreads ="250" strategy ="ms" maxHttpHeaderSize ="8192"
emptySessionPath ="true"
enableLookups ="false" redirectPort ="8443" acceptCount ="100"
connectionTimeout ="20000" disableUploadTimeout ="true" />
把port="8080"改成port="80",这样JBoss就可以使用80端口号了
三、测试例子
打开mysql,使用test数据库,创建一个表mytable,并插入几条记录,命令如下:
CREATE TABLE mytable (name VARCHAR(20), sex CHAR(1), addr VARCHAR(30));
insert into mytable(name, sex, addr) values("plusboy", 'M', "Guangzhou");
insert into mytable(name, sex, addr) values("YanDingcheng", 'M', "Guangzhou");
编写一个myTest.jsp程序用于测试,把该文件放到目录
$JBOSS_HOME/server/default/deploy/jbossweb-tomcat55.sar/ROOT.war
下面。
文件名:myTest.jsp
<%
@ page contentType
=
"
text/html; charset=gb2312
"
%>
<% @ page language = " java " %>
<% @ page import = " com.mysql.jdbc.Driver " %>
<% @ page import = " java.sql.* " %>
<%
String driverName = " com.mysql.jdbc.Driver " ;
String userName = " root " ;
String userPasswd = "" ;
String dbName = " test " ;
String tableName = " mytable " ;
String url = " jdbc:mysql://lvs1/ " + dbName + " ?user= " + userName + " &password= " + userPasswd;
Class.forName(driverName).newInstance();
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql = " SELECT * FROM " + tableName;
ResultSet rs = statement.executeQuery(sql);
// 获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
// 确定数据集的列数,亦字段数
int numColumns = rmeta.getColumnCount();
// 输出每一个数据值
out.print( " name " );
out.print( " | " );
out.print( " sex " );
out.print( " | " );
out.print( " addr " ); out.print( " <br> " );
while (rs.next()) {
out.print(rs.getString( 1 ) + " " );
out.print( " | " );
out.print(rs.getString( 2 ) + " " );
out.print( " | " );
out.print(rs.getString( 3 ));
out.print( " <br> " );
}
out.print( " <br> " );
out.print( " 数据库操作成功,恭喜你 " );
rs.close();
statement.close();
connection.close();
%>
<% @ page language = " java " %>
<% @ page import = " com.mysql.jdbc.Driver " %>
<% @ page import = " java.sql.* " %>
<%
String driverName = " com.mysql.jdbc.Driver " ;
String userName = " root " ;
String userPasswd = "" ;
String dbName = " test " ;
String tableName = " mytable " ;
String url = " jdbc:mysql://lvs1/ " + dbName + " ?user= " + userName + " &password= " + userPasswd;
Class.forName(driverName).newInstance();
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql = " SELECT * FROM " + tableName;
ResultSet rs = statement.executeQuery(sql);
// 获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
// 确定数据集的列数,亦字段数
int numColumns = rmeta.getColumnCount();
// 输出每一个数据值
out.print( " name " );
out.print( " | " );
out.print( " sex " );
out.print( " | " );
out.print( " addr " ); out.print( " <br> " );
while (rs.next()) {
out.print(rs.getString( 1 ) + " " );
out.print( " | " );
out.print(rs.getString( 2 ) + " " );
out.print( " | " );
out.print(rs.getString( 3 ));
out.print( " <br> " );
}
out.print( " <br> " );
out.print( " 数据库操作成功,恭喜你 " );
rs.close();
statement.close();
connection.close();
%>
重启JBoss和MySql。
在浏览器地址栏输入http://localhost/myTest.jsp
哈哈,数据库操作成功。
四、解决MySql重启后JBoss重连问题
jboss的连接池支持根据SQLException的类型来确定是应用的异常还是不可恢复的数据库连接的异常,因此可以在
datasource 的配置里加上
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
这样就不需要每次获取连接的时候都执行一次查询了,性能提高了很多,
只有当发生SQLException并且他侦测到这个异常导致连接无法使用时才会丢弃这个连接
更多推荐
已为社区贡献1条内容
所有评论(0)