How to configure transaction management for working with 2 different db in Spring?

The javadoc for JpaTransactionManager has some advice on this: This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make … Read more

Django – how to specify a database for a model?

You can’t specify a database for a model, but you can define it in a custom DB router class. # app/models.py class SomeModel(models.Model): … # app/dbrouters.py from app.models import SomeModel … class MyDBRouter(object): def db_for_read(self, model, **hints): “”” reading SomeModel from otherdb “”” if model == SomeModel: return ‘otherdb’ return None def db_for_write(self, model, **hints): … Read more

How to use 2 or more databases with spring?

Here is the example code for having multiple Database/datasource on Spring-Boot I hope it helps! application.properties spring.ds_items.driverClassName=org.postgresql.Driver spring.ds_items.url=jdbc:postgresql://srv0/test spring.ds_items.username=test0 spring.ds_items.password=test0 spring.ds_users.driverClassName=org.postgresql.Driver spring.ds_users.url=jdbc:postgresql://srv1/test spring.ds_users.username=test1 spring.ds_users.password=test1 DatabaseItemsConfig.java package sb; import org.springframework.boot.autoconfigure.jdbc.TomcatDataSourceConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @Configuration @ConfigurationProperties(name = “spring.ds_items”) public class DatabaseItemsConfig extends TomcatDataSourceConfiguration { @Bean(name = “dsItems”) public DataSource dataSource() … Read more

How to run the same query on all the databases on an instance?

Try this one – SET NOCOUNT ON; IF OBJECT_ID (N’tempdb.dbo.#temp’) IS NOT NULL DROP TABLE #temp CREATE TABLE #temp ( [COUNT] INT , DB VARCHAR(50) ) DECLARE @TableName NVARCHAR(50) SELECT @TableName=”[dbo].[CUSTOMERS]” DECLARE @SQL NVARCHAR(MAX) SELECT @SQL = STUFF(( SELECT CHAR(13) + ‘SELECT ‘ + QUOTENAME(name, ””) + ‘, COUNT(1) FROM ‘ + QUOTENAME(name) + ‘.’ … Read more