Configuring MySQL DataSource using @DataSourceDefinition in JBoss AS7
Asked Answered
V

3

8

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]"]}}}

Vichyssoise answered 26/2, 2013 at 22:36 Comment(0)
V
4

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
    }
}
Vichyssoise answered 27/2, 2013 at 9:11 Comment(0)
M
0

One suggestion is that configure data source name in standalone.xml or domain.xml in jboss as 7. and configure mysql connector jar in jboss module.And try .

Configure Mysql Connector jar Refer this link.

http://www.mastertheboss.com/jboss-datasource/how-to-configure-a-datasource-with-jboss-7

Data source configuration in Standalone.xml Refer this link

https://docs.jboss.org/author/display/AS71/DataSource+configuration

Moke answered 27/2, 2013 at 5:4 Comment(2)
I have already mentioned that I am able to configure DataSource in standalone.xml. I am looking for @DataSourceDefinition based config.Vichyssoise
Have you configured Mysql Connector Jar in Jboss Modules ?Moke
M
0

Also the next problem that you can have is:

java.lang.ClassCastException: com.mysql.jdbc.Driver cannot be cast to javax.sql.DataSource

Cause param className of @DataSourceDefinition must provide DataSource implementation (see javadoc), not driver class.

For MySQL it can be:

className = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
className = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"
className = "com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"    // XA transaction
Mcilroy answered 12/4, 2016 at 23:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.