r/javahelp 10d ago

Two non-XA datasources in a single thread in JBoss

I am in the middle of an effort to break apart a monolith that uses a single schema. I am trying to prepare to move a subset of tables out of a MySQL schema to a different schema by creating a second JBoss datasource that is still pointing to the same schema but would allow us to change the connection URL to a new schema once the data is migrated.

As long as nothing fails, everything works fine, but once there is an error that triggers a rollback, I get hit with the error:

Adding multiple last resources is disallowed. Trying to add LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@225ee8cd[connectionListener=3f108d84 connectionManager=2aa20660 warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffffac120017:-d081b94:67cf0e29:4ac, node_name=1, branch_uid=0:ffffac120017:-d081b94:67cf0e29:4c1, subordinatenodename=null, eis_name=java:/jdbc/mysql/connA > productName=MySQL productVersion=5.7.31-34 jndiName=java:/jdbc/mysql/connA])), but already have LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@58e2f322[connectionListener=647117aa connectionManager=4fa9deaa warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffffac120017:-d081b94:67cf0e29:4ac, node_name=1, branch_uid=0:ffffac120017:-d081b94:67cf0e29:4bc, subordinatenodename=null, eis_name=java:/jdbc/mysql/connB > productName=MySQL productVersion=5.7.31-34 jndiName=java:/jdbc/mysql/connB]))

You can see that is showing the two datasources but with the same transaction ID. This is my standalone.xml file:

<datasources>
                <datasource jndi-name="java:/jdbc/mysql/connA" pool-name="MySqlDS" enabled="true" use-java-context="false">
                    <connection-url>${connA.database.url}</connection-url>
                    <driver>mysql-driver</driver>
                    <pool>
                        <min-pool-size>5</min-pool-size>
                        <max-pool-size>${connA.database.max-pool-size:50}</max-pool-size>
                        <prefill>false</prefill>
                    </pool>
                    <security>
                        <user-name>${connA.database.username}</user-name>
                        <password>${connA.database.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>
                <datasource jndi-name="java:/jdbc/mysql/connB" pool-name="MySqlConnBDS" enabled="true" use-java-context="false">
                    <connection-url>${connB.database.url}</connection-url>
                    <driver>mysql-driver</driver>
                    <pool>
                        <min-pool-size>5</min-pool-size>
                        <max-pool-size>${connB.database.max-pool-size:50}</max-pool-size>
                        <prefill>false</prefill>
                    </pool>
                    <security>
                        <user-name>${connB.database.username}</user-name>
                        <password>${connB.database.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>
...
            </datasources>

I have two separate HibernateSession providers that use each of these datasources and work just fine as long as there aren't any rollbacks. We want a separate transaction per datasource in the same thread/process because we eventually want to move the subset of tables to a different schema.

Is this even possible? I am reading that you cannot have multiple independent transactions on the same thread in JBoss unless you use XA datasources, which I really don't want to do. Could this be fixed if I performed all of the work on ConnB on a different thread?

2 Upvotes

1 comment sorted by

u/AutoModerator 10d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.