Tuesday, September 4, 2012

Datasource configuration with Sql server 2008 in jboss 7.1.1 with jdk7

1.Configure the Datasource in standalone.xml
C:\Program Files\jboss-as-7.1.1.Final\standalone\configuration\standalone.xml



add below code under subsystem
<datasource jndi-name="java:jboss/datasources/SampleDS" pool-name="SampleDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:sqlserver://localhost\\SQLEXPRESS/Databases/EPS:4718;databaseName=SampleDB</connection-url>
                    <driver>sqljdbc</driver>
<pool>

            <min-pool-size>10</min-pool-size>

                <max-pool-size>100</max-pool-size>

                <prefill>true</prefill>

            </pool>
                    <security>
                        <user-name>sa</user-name>
                        <password>xxx</password>
                    </security>
                </datasource>

<driver name="sqljdbc" module="com.microsoft.sqlserver.jdbc">
                        <xa-datasource-class>com.microsoft.sqlserver.jdbc.SQLServerDataSource</xa-datasource-class>
                    </driver>


Port Number will be Dynamic port of Your Sql Server 2008

configure module info
create module.xml file under 
C:\Program Files\jboss-as-7.1.1.Final\modules

create subdirectory:
C:\Program Files\jboss-as-7.1.1.Final\modules\com\microsoft\sqlserver\jdbc\main

place module.xml and sqljdbc4.jar file

module.xml
.......
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.microsoft.sqlserver.jdbc">
  <resources>
    <resource-root path="sqljdbc4.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
     <module name="javax.servlet.api" optional="true"/>
  </dependencies>
</module>


and invoke from java code
..................................

InitialContext context = new InitialContext();
 DataSource dataSource = (DataSource) context.lookup("java:jboss/datasources/SampleDS");
Connection connection = dataSource.getConnection();