简体   繁体   中英

WildFly8 EJB3 MySQL load driver don't work

Hi I use IDE Eclipse Luna,JavaSE8.40,WildFly8.2,MySQL5.6.23 and Windows7 32bit. I would like with @Stateful bean connect do MySQL server. When I use datasource it´s work correctly(I create datasource throw webconsole). I would like connect to database without datasource.I have a problem with load the driver.

This work correctly

    public class ServerBean03 implements Rozhrani03 {
        private Context ctx;
        private DataSource dataSource;
        private Connection pripoj;
        private Statement dotaz;
        private ResultSet vysledek;
        @PostConstruct
        private void init(){
            System.out.println("Metoda Init()");
            try {
                ctx = new InitialContext();
                dataSource = (DataSource) ctx.lookup("java:jboss/jdbc/MySQLcon");           
                pripoj = dataSource.getConnection();                
                dotaz = pripoj.createStatement();           }                               
            catch (SQLException | NamingException e) {
                System.out.println("Nepodarilo se vyrobit novou databazi");
                e.printStackTrace();    }   }

This work correctly too

public class ServerBean02 implements Rozhrani02 {
    private Connection pripoj;
    private Statement dotaz;
    private ResultSet vysledek;
    @Resource(lookup="java:jboss/jdbc/MySQLcon")  // nebo  @Resource(name="MySQLcon")
    private javax.sql.DataSource dataSource;
    @PostConstruct
    private void init(){
        System.out.println("Metoda Init()");
        try {
            pripoj = dataSource.getConnection();                        
            dotaz = pripoj.createStatement();   }                       
        catch (SQLException e) {
            System.out.println("Nepodarilo pripojit na novou databazi");
            e.printStackTrace();    }   }

This not work

public class ServerBean01 implements Rozhrani01{
    private Connection pripoj;
    private Statement dotaz;
    @PostConstruct
    private void init(){
        System.err.println("Pokusime se nacist driver");
        try {
            Class.forName("com.mysql.jdbc.Driver"); }       //  Here the Bean is crash 
        catch (ClassNotFoundException e) {
            System.err.println("Nepodarilo se nacist Driver");
            e.printStackTrace();    } 
        System.err.println("Pokusime se pripojit na databazi");
        try {
            pripoj = DriverManager.getConnection("jdbc:mysql://localhost/test", "Java2", "asdasdasd");      
            dotaz = pripoj.createStatement();   }                                                           
        catch (SQLException e) {
            System.err.println("Nepodarilo se pripojit k databazi");
            e.printStackTrace();    }   }


WildFly8.2 I install in ProgramFiles. I create few folders in folder module.
It´s look like so : C:\\Program Files\\JavaEE WildFly8\\modules\\com\\mysql\\main
1) I copy there MySQL connector - mysql-connector-java-5.1.34-bin.jar - to folder main
2) I create module.xml - in folder main

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

3) I edit the C:\\Program Files\\JavaEE WildFly8\\standalone\\configuration\\standalone.xml

<subsystem xmlns="urn:jboss:domain:datasources:2.0">
        <datasources>
            <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                <driver>h2</driver>
                <security>
                    <user-name>sa</user-name>
                    <password>sa</password>
                </security>
            </datasource>
            <datasource jta="true" jndi-name="java:jboss/jdbc/MySQLcon" pool-name="MySQLcon" enabled="true" use-ccm="true">
                <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
                <driver-class>com.mysql.jdbc.Driver</driver-class>
                <driver>mysql-connector-java-5.1.34-bin.jar_com.mysql.jdbc.Driver_5_1</driver>
                <security>
                    <user-name>Java2</user-name>
                    <password>asdasdasd</password>
                </security>
                <validation>
                    <validate-on-match>false</validate-on-match>
                    <background-validation>false</background-validation>
                </validation>
                <timeout>
                    <set-tx-query-timeout>false</set-tx-query-timeout>
                    <blocking-timeout-millis>0</blocking-timeout-millis>
                    <idle-timeout-minutes>0</idle-timeout-minutes>
                    <query-timeout>0</query-timeout>
                    <use-try-lock>0</use-try-lock>
                    <allocation-retry>0</allocation-retry>
                    <allocation-retry-wait-millis>0</allocation-retry-wait-millis>
                </timeout>
                <statement>
                    <share-prepared-statements>false</share-prepared-statements>
                </statement>
            </datasource>
            <drivers>
                <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
                <driver name="mysqlDriver" module="com.mysql">
                    <xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
                    <driver-class>com.mysql.jdbc.Driver</driver-class>
                </driver>
            </drivers>
        </datasources>
    </subsystem>

Have I do something bad? Or have I forgot something?

You need to add a driver module dependency to your application in a jboss-deployment-structure.xml as described in Class Loading in WildFly

Eg:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="com.mysql" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

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