views:

76

answers:

1

Thanks for reading this.

I have 2 MySQL databases - master for writes, slave for reads. The perfect scenario I imagine is that my app uses connection to master for readOnly=false transactions, slave for readOnly=true transactions.

In order to implement this I need to provide a valid connection depending on the type of current transaction. My data service layer should not know about what type of connection it uses and just use the injected SqlMapClient (I use iBatis) directly. This means that (if I get it right) the injected SqlMapClients should be proxied and the delegate should be chosen at runtime.

public class MyDataService {

    private SqlMapClient sqlMap;

    @Autowired
    public MyDataService (SqlMapClient sqlMap) {
        this.sqlMap = sqlMap;
    }

    @Transactional(readOnly = true)
    public MyData getSomeData() {
        // an instance of sqlMap connected to slave should be used
    }

    @Transactional(readOnly = false)
    public void saveMyData(MyData myData) {
        // an instance of sqlMap connected to master should be used
    }
}

So the question is - how can I do this?

Thanks a lot

+3  A: 

It's an interesting idea, but you'd have a tough job on your hands. The readOnly attribute is intended as a hint to the transaction manager, and isn't really consulted anywhere meaningful. You'd have to rewrite or extend multiple Spring infrastructure classes.

So unless you're hell-bent on getting this working a you want, your best option is almost certainly to inject two separate SqlMapClient objects into your DAO, and for the methods to pick the appropriate one. The @Transactional annotations would also need to indicate which transaction manager to use (assuming you're using DataSourceTransactionManager rather than JpaTransactionManager), taking care to match the transaction manager to the DataSource used by the SqlMapClient.

skaffman
Is there an easier way with AspectJ for example?
artemb
@artemb: I.m sure AspectJ would help, but it's still not going to be easy.
skaffman