最近使用jboss-4.2.2.GA版本配置数据源,略有心得,特发此篇,希望对还在迷茫的同学有帮助!
各种类型的数据库,都可以根据%JBOSS_HOME%/docs/examples/jca/下的示例文档配置,
下面以mssql为例,提供我们在生产机环境的配置。
该xml文件存在于%JBOSS_HOME%/docs/examples/jca/下的mssql-ds.xml,
修改文件内容形如
<
datasources
>
< local-tx-datasource >
< jndi-name > dataSource </ jndi-name >
< connection-url > jdbc:microsoft:sqlserver://localost:1433;DatabaseName=jcwork </ connection-url >
< driver-class > com.microsoft.jdbc.sqlserver.SQLServerDriver </ driver-class >
< user-name > sa </ user-name >
< password ></ password >
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
< metadata >
< type-mapping > MS SQLSERVER2000 </ type-mapping >
</ metadata >
</ local-tx-datasource >
</ datasources >
< local-tx-datasource >
< jndi-name > dataSource </ jndi-name >
< connection-url > jdbc:microsoft:sqlserver://localost:1433;DatabaseName=jcwork </ connection-url >
< driver-class > com.microsoft.jdbc.sqlserver.SQLServerDriver </ driver-class >
< user-name > sa </ user-name >
< password ></ password >
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
< metadata >
< type-mapping > MS SQLSERVER2000 </ type-mapping >
</ metadata >
</ local-tx-datasource >
</ datasources >
然后将其拷贝至%JBOSS_HOME%/server/all(or default)/deploy/下,
对应的数据库驱动程序jar需copy至部署的配置的lib下,本例中就要拷贝mssqlserver.jar,msbase.jar,msutil.jar到该目录下
以下是在jsp页面中测试该数据源的代码
Context ctx
=
null
;
Connection cnn = null ;
java.sql.Statement stmt = null ;
ResultSet rs = null ;
try
{
ctx = new InitialContext();
if (ctx == null )
throw new Exception( " initialize the Context failed " );
DataSource ds = (DataSource)ctx.lookup( " java:dataSource " );
out.println(ds);
if (ds == null )
throw new Exception( " datasource is null " );
try {
cnn = ds.getConnection();
out.println( " <br> connection: " + cnn + " 你已经成功得到了数据源! " );
} catch (Exception e){
e.printStackTrace();
}
}
finally
{
if (rs != null )
rs.close();
if (stmt != null )
stmt.close();
if (cnn != null )
cnn.close();
if (ctx != null )
ctx.close();
}
Connection cnn = null ;
java.sql.Statement stmt = null ;
ResultSet rs = null ;
try
{
ctx = new InitialContext();
if (ctx == null )
throw new Exception( " initialize the Context failed " );
DataSource ds = (DataSource)ctx.lookup( " java:dataSource " );
out.println(ds);
if (ds == null )
throw new Exception( " datasource is null " );
try {
cnn = ds.getConnection();
out.println( " <br> connection: " + cnn + " 你已经成功得到了数据源! " );
} catch (Exception e){
e.printStackTrace();
}
}
finally
{
if (rs != null )
rs.close();
if (stmt != null )
stmt.close();
if (cnn != null )
cnn.close();
if (ctx != null )
ctx.close();
}
之后启动jboss服务,如果一切顺利可以看到如下输出:
org.jboss.resource.adapter.jdbc.WrapperDataSource@10d78ec
connection:org.jboss.resource.adapter.jdbc.WrappedConnection@1ccf342你已经成功得到了数据源!