简体   繁体   中英

Jboss connection pool doesn't connect with the DataBase

I am using JBoss 7.1, and I'm trying to create a Database Connection Pool through Application Server. I'm using a mysql DB, but I tried also with a Postgre database and the error is just the same. Everything seems to be OK, but when I try to access to the application, it appears an error on console that advises me that the connection with the database was failed. My code is the following:

My Jboss standalone.xml.

<datasource jndi-name="java:jboss/jdbc/exampleDS" pool-name="jdbc/exampleDS" enabled="true" use-java-context="true" use-ccm="true">
                    <connection-url>jdbc:mysql://127.0.0.1:3306/example?useSSL=false</connection-url>
                    <driver>mysql-connector-java-5.1.43-bin.jar</driver>
                    <pool>
                        <min-pool-size>10</min-pool-size>
                        <max-pool-size>100</max-pool-size>
                        <prefill>true</prefill>
                    </pool>
                    <security>
                        <user-name>root</user-name>
                        <password>password</password>
                    </security>
                    <validation>
                        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
                        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
                    </validation>
                </datasource>
<driver name="mysql-connector-java-5.1.43-bin.jar" module="com.mysql">
                        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</xa-datasource-class>
                    </driver>

My web.xml

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

My javacode

private ServiceLocator() {
    try {
        this.initCtx = new InitialContext();
        this.envCtx = (Context) initCtx.lookup("java:comp/env");
        DataSource ds = (DataSource)
                envCtx.lookup("jdbc/exampleDS");
    } catch (NamingException e) {
        ApplicationException apRE = new ApplicationException(ErrorCode.E_JDNI001, e);
        LogService.getLog().fatal(apRE.getMessage(), apRE);
        throw apRE;
    }
}

Finally, when I start my jboss server, none error appears, but the console said that the tables of my DB don't exist.

I appreciate help <3

I had fixed the error. First thing to do is creating a jboss-web.xml where web.xml, in my case, webapp/WEB-INF. It must be similar to the following:

<jboss-web>    
  <resource-ref>  
    <res-ref-name>jdbc/exampleDS</res-ref-name>  
    <jndi-name>java:jboss/jdbc/exampleDS</jndi-name> 
  </resource-ref>  
</jboss-web>  

Once that is created, we must create a module.xml inside of modules->com->mysql->main, containing the following:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysql">
<resources>
    <resource-root path="mysql-connector-java-5.1.43-bin.jar"/>
</resources>
<dependencies>
   <module name="javax.api"/>
   <module name="javax.transaction.api"/>
</dependencies>
</module>

You have to insert also, the .jar that you used also inside of the main dir. Regarding the javacode, you need to change the initCtx.lookup("java:comp/env"); for this: initCtx.lookup("java:");

That's all :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM