How do I configure our MySQL ReplicationDriver for

2019-07-04 05:10发布

问题:

We’re using MySql 5.5.37 and JBoss 7.1.3, and mysql-connector-java-5.1.22-bin.jar. How do I configure my JBoss’ standalone.xml datasource to connect to our master-slave configuration for MySQL? I tried the below

            <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:mysql:replication//master.amazonaws.com:3306,slave.amazonaws.com:3306/dbsid?failOverReadOnly=true;roundRobinLoadBalance=true</connection-url>
                <driver>mysql</driver>
                <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                <pool>
                    <min-pool-size>10</min-pool-size>
                    <max-pool-size>100</max-pool-size>
                    <prefill>true</prefill>
                </pool>
                <security>
                    <user-name>sb</user-name>
                    <password>sb</password>
                </security>
                <statement>
                    <prepared-statement-cache-size>32</prepared-statement-cache-size>
                    <share-prepared-statements>true</share-prepared-statements>
                </statement>
            </datasource>
            <drivers>
                <driver name="mysql" module="com.mysql">
                    <xa-datasource-class>com.mysql.jdbc.ReplicationDriver</xa-datasource-class>
                </driver>
                <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
            </drivers>

But when I restart my server, I get the exception …

Caused by: javax.resource.ResourceException: Wrong driver class [class com.mysql.jdbc.Driver] for this connection URL [jdbc:mysql:replication//master.amazonaws.com:3306,slave.amazonaws.com:3306/dbsid?failOverReadOnly=true;roundRobinLoadBalance=true]
        at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:256)
        ... 47 more

I opened my JAR and verified the ReplicationDriver class is in there. Not sure what else I should be trying.

回答1:

For me this works:

<driver name="com.mysql" module="com.mysql">
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>


回答2:

Not sure why I was messing around with xa-datasource-class elements, but what worked for me was

                <driver name="mysql" module="com.mysql">
                    <driver-class>com.mysql.jdbc.ReplicationDriver</driver-class>
                </driver>


回答3:

The driver implementation is selected based on the connect-url.

If you specify a connection url like this: "jdbc:mysql:replication//..", MySQL connector/J returns the ReplicationDriver implementation.

If you set the driver-class as com.mysql.jdbc.ReplicationDriver all your datasources are decoded as "replication url" and must always contains a master and at least one slave.

I think is better to choose the correct implementation with the url customization (replication, loadbalance, fabric...).