JooQ spring Loading multiple schemas (read write)

I want to use jooQ with spring boot. I have my own slave architecture, so I want to use jooQ with a master slave architecture (master for writing and slave for reading).

How should I proceed. now i am using pom.xml for jooQ setup

<configuration>
    <jdbc>
        <driver>com.mysql.jdbc.Driver</driver>
        <url>${datasource.primary.url}</url>
        <user>${datasource.primary.username}</user>
        <password>${datasource.primary.password}</password>
    </jdbc>
    <generator>
        <name>org.jooq.util.DefaultGenerator</name>
        <database>
            <name>org.jooq.util.mysql.MySQLDatabase</name>
            <includes>.*</includes>
            <excludes/>
            <inputSchema>jpa</inputSchema>
        </database>
        <generate>
            <deprecated>false</deprecated>
        </generate>
        <target>
            <packageName>com.gensrc.model</packageName>
            <directory>src/main/java</directory>
        </target>
    </generator>
</configuration>

      

and then just Autowiring DSLContext

But how do I change my code now.

+3


source to share


1 answer


From your comments:

I want to redirect all my write requests to master and all my read requests to slave.

I'm not sure if this difficult distinction between write = master, read = slave would be a good idea. Sometimes, you may have to read and with the master within the same transaction boundary (i.e., Read uncommitted data) in case sending a read request to the slave produces incorrect results.



Thus, the best place to implement this routing would be to have separate instances DataSource

and insert them into jOOQ accordingly. Then you will separate master and slave in your service layer explicitly, transparently to jOOQ.

You could, of course, implement your original requirement using jOOQ ExecuteListener

and provide jOOQ with the correct JDBC Connection

depending on the request type ( ExecuteContext.type()

), but again, I think this will end soon.

0


source







All Articles