Search code examples
javamysqljava-ee-6jboss7.x

Configuring MySQL DataSource using @DataSourceDefinition in JBoss AS7


I am able to configure MySQL DataSource in standalone.xml and is working fine. But I want to configure DataSource using the @DataSourceDefinition annotation.

How to configure a MySQL datasource using @DataSourceDefinition in JBoss AS7?

What I have already tried is:

@DataSourceDefinition(
        className = "com.mysql.jdbc.Driver",
        name = "java:global/jdbc/MyDS",
        serverName="localhost",
        portNumber=3306,
        user = "root",
        password = "admin",
        databaseName = "test"
)
@Startup
public class DBConfig {
}

together with this persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="javaee6-app" transaction-type="JTA">
        <jta-data-source>java:global/jdbc/MyDS</jta-data-source>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

I have the mysql connector jar file in WEB-INF/lib.

But when I deploy the application I am getting this error:

(DeploymentScanner-threads - 2) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.persistenceunit.\"javaee6-app.war#javaee6-app\"jboss.naming.context.java.global.jdbc.MyDSMissing[jboss.persistenceunit.\"javaee6-app.war#javaee6-app\"jboss.naming.context.java.global.jdbc.MyDS]"]}}}


Solution

  • I figured it out myself.

    It seems JBoss AS7 scanning process has some flaws. As per Java EE 6 spec it should scan @DataSourceDefinition annotations on any classes. But it is working fine if we put it on a class which has a @Stateless annotation.

    @DataSourceDefinition(
            className = "com.mysql.jdbc.Driver",
            name = "java:global/jdbc/MyDS",
            serverName="localhost",
            portNumber=3306,
            user = "root",
            password = "admin",
            databaseName = "test"
    )
    @Stateless
    public class DBConfig {
        public void test() { //there should be atleast one method, so this dummy
        }
    }