This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@require_GET | |
def order_detail(request, id): | |
order = get_object_or_404(Order, id=id) | |
return JsonResponse(model_to_dict(order), safe=False) | |
@require_POST | |
@csrf_exempt | |
def create_order(request): | |
form = CreateOrder(json.loads(request.body)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package database | |
const ( | |
Primary = "primary" | |
Secondary = "secondary" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Constants { | |
public static final String PRIMARY = "primary"; | |
public static final String SECONDARY = "secondary"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RequestMapping("/api/v1/orders") | |
@RestController | |
@Database(Constants.SECONDARY) | |
public class OrderController { | |
private final OrderService orderService; | |
public OrderController(OrderService orderService) { | |
this.orderService = orderService; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableTransactionManagement | |
@EnableJpaRepositories(basePackages = {"com.github.sonus21.readwrite.repositories"}) | |
@EntityScan(basePackages = "com.github.sonus21.readwrite.models.entities") | |
public class SqlDatabaseConfiguration { | |
private final DataSourceSecondaryConfig dataSourceSecondaryConfig; | |
private final DataSourcePrimaryConfig dataSourcePrimaryConfig; | |
@Autowired |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatabaseRouter extends AbstractRoutingDataSource { | |
@Override | |
protected Object determineCurrentLookupKey() { | |
return DatabaseContext.get(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatabaseContext { | |
private static final ThreadLocal<String> databaseName = new ThreadLocal<>(); | |
public static void set(String name) { | |
databaseName.set(name); | |
} | |
public static void clear() { | |
databaseName.remove(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Aspect | |
@Component | |
public class DatabaseAspect { | |
/** | |
* AOP joint point handler that will be invoked by AOP when it encounters a method annotated with Database | |
* or the class containing this method is annotated with Database. | |
* | |
* @param jointPoint joint point object | |
* @return the result of joint point | |
* @throws Throwable if any exception occurred while handling joint point |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Database annotation that should be used at controller,worker,consumer,methods or classes | |
* to route the database traffic to appropriate node. | |
*/ | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ElementType.TYPE, ElementType.METHOD}) | |
public @interface Database { | |
String value() default ""; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getPrimaryDbConfig() database.MySqlConfig { | |
// use any other mechanism like viper/toml/env file to create the cpnfig | |
return database.MySqlConfig{ | |
Host: "localhost", | |
Port: 3306, | |
Database: "my_app_db", | |
Username: "my_app", | |
Password: "my_app_pass", | |
MaxConnectionRetries: 3, | |
MaxOpenConnection: 30, |
NewerOlder