This file contains 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
/* | |
* Copyright (c) 2023 Sonu Kumar, sonunitw12@gmail.com | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* You may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
This file contains 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
#Adapted from https://djangosnippets.org/snippets/2619/ | |
@register.tag(name='counter') | |
def do_counter(parser, token): | |
""" | |
Counter tag. Can be used to output and increment a counter. | |
Usage: | |
- {% counter %} to output and post-increment the counter variable | |
- {% counter reset %} to reset the counter variable to 1 |
This file contains 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
import threading | |
threadLocal = threading.local() | |
def get_current_db_name(): | |
return getattr(threadLocal, "DB", None) | |
def set_db_for_router(db): |
This file contains 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
INSTALLED_APPS = [ | |
# ... | |
"CoreApp" | |
# ... | |
] | |
MIDDLEWARE = [ |
This file contains 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
from functools import wraps | |
from .utils import db_from_the_request | |
from .db_router import set_db_for_router, clear_db_for_router | |
class DatabaseMiddleware(object): | |
""" | |
DatabaseMiddleware sets the db in the thread local context and used | |
across multiple calls to handle the DB calls. |
This file contains 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
from .constants import SECONDARY, PRIMARY | |
from CoreApp.middlewares import db_wrapper | |
@db_wrapper(PRIMARY) | |
def reserve_driver(request): | |
# logic goes here | |
pass | |
This file contains 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 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 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 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 |
NewerOlder