0

I have a problem to get connection to my database via java and jboss esb 4.2. I have build a .war package and deployed it on the jboss server. But i am gettin error while trying to connect to the database.

Java Method

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource) envContext.lookup("jdbc/VINService");

    String sql = "SELECT ...";
    conn = ds.getConnection();
    stmt = conn.prepareStatement(sql);
...

jboss-web.xml

<resource-ref>
    <res-ref-name>jdbc/VINService</res-ref-name>
    <jndi-name>java:jdbc/VINService</jndi-name>
    <res-type>javax.sql.DataSource</res-type>
</resource-ref>

web.xml

<resource-ref>
    <description>VINService</description>
    <res-ref-name>jdbc/VINService</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

VIN_Service-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
   <local-tx-datasource>
      <jndi-name>jdbc/VINService</jndi-name>
      <connection-url>jdbc:mysql://url/db?autoReconnect=true</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>name</user-name>
      <password>password</password>
      <min-pool-size>5</min-pool-size>
      <max-pool-size>20</max-pool-size>
      <idle-timeout-minutes>0</idle-timeout-minutes>
      <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
      <check-valid-connection-sql>SELECT 1 FROM vehiclecache limit 1</check-valid-connection-sql>
  </local-tx-datasource>
</datasources>

What am i doing wrong? I am getting Nullpointer Error at

conn = ds.getConnection()

so it isn't finding the datasource. Can anybody help me?

drogon
  • 11
  • 4
Anton Styopin
  • 753
  • 4
  • 17
  • 35
  • It's simple If you getting Nullpointer Error at conn = ds.getConnection(); , This means this method **ds.getConnection()** returns **null**, Don't doubt on JVM –  Oct 06 '17 at 09:48
  • https://stackoverflow.com/questions/4459465/how-can-we-configure-database-connection-from-jboss –  Oct 06 '17 at 09:54
  • But why is it null? I have configured it and my database is accessable. – Anton Styopin Oct 06 '17 at 10:04
  • jdbc/VINService and check above link –  Oct 06 '17 at 10:06
  • I checked the link and i did the things! Doesn't work either. – Anton Styopin Oct 06 '17 at 10:18
  • Can you go through this by these step https://developer.jboss.org/wiki/DataSourceConfigurationInAS7?_sscc=t It will must resolve you Connection issue. –  Oct 06 '17 at 10:58
  • But i have not JBossAS7. I have JBoss ESB 4.2 – Anton Styopin Oct 06 '17 at 11:08
  • JBossESB is the next generation of EAI But This is not the issue, Java use common configuration to connect with any database. http://www.mastertheboss.com/soa-cloud/jboss-soa/jboss-esb-tutorial?start=1 –  Oct 06 '17 at 11:22

0 Answers0