作为我的应用程序的一部分,我必须在Spring Service中维护多个数据库。所以我动态地创建了Datasource
并在Map上维护。
每当客户端服务请求时,我都会从Map中提取数据源(在使用Datasource
验证dataSource.getConnection().isValid(1)
之后)并执行业务逻辑。
我担心的是,
@Bean(destroyMethod = "close")
?Datasource
的最佳方法是什么。 还有其他最适合我需要的方法吗?
@Service
public class ClientDataSourceFactory {
private Map<String, DataSource> map = new ConcurrentHashMap<String, DataSource>();
public void getDataSource() {
// Pre - Business Logic
synchronized (this) {
dataSource = map.get(key);
boolean isValid = true;
try (Connection con = dataSource.getConnection()) {
isValid = con.isValid(1);
} catch (SQLException e) {
isValid = false;
}
if (!isValid) {
// TODO: Need to close properly
// ???
map.remove(key);
dataSource = null;
// Create Datasource again using createDatasource()
// Store Datasource at Map with key
}
// Business Logic
}
@PreDestroy
void preDestroy(){
// TODO: Close all existing Datasources
// ???
}
Datasource createDatasource(String key) {
// fetch details using key
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setUrl(url);
dataSource.setDriverClassName(driverName);
return dataSource;
}
}
答案 0 :(得分:0)
您可以按如下方式注入多个数据源,首先配置所有数据源
<?php
include 'connection.php';
$getid = $_GET['getid'];
if (isset($_POST['userID'])) {
$userID = $_POST['userID'];
} else {
$error = true;
}
if (isset($_POST['emailAddress'])) {
$emailAddress = $_POST['emailAddress'];
} else {
$error = true;
}
if (isset($_POST['firstName'])) {
$firstName = $_POST['firstName'];
} else {
$error = true;
}
if (isset($_POST['lastName'])) {
$lastName = $_POST['lastName'];
} else {
$error = true;
}
if (isset($_POST['accessLevel'])) {
$accessLevel = $_POST['accessLevel'];
} else {
$error = true;
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
} else {
$error = true;
}
if (!$error) {
$update = "UPDATE users SET userID='$userID',
emailAddress='$emailAddress',firstName='$firstName', lastName='$lastName'accessLevel ='$accesslevel',password='$password' WHERE userID = '$getid'";
$return = mysqli_query($conn, $update) or die(mysqli_errno($conn));
} else {
"something wrong";
}
$returnQuery = "SELECT * FROM users WHERE userID='$getid'";
$return = mysqli_query($conn, $returnQuery);
if (mysqli_num_rows($return) > 0) {
while ($row = mysqli_fetch_assoc($return)) {
$userEmail = $row['emailAddress'];
$userID = $row['userID'];
}
}
$content = "
Please use the following link to login in with your
new details:
http://xxxxxxxxxxxxxxx@xxxxxxx.co.uk
Please make a note of your new User name,
you will need this to log in to CaterVLE= $userID
";
$send = $userEmail;
$subject = "Log in Details";
$headers = "Content-type-type: text/html";
mail($send, $subject, $content, $headers);
?>
现在,在您的服务中注入您需要的数据源,。
@Configuration
public class Config {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.datasource.secondary")
@Qualifier("anotherDataSource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
现在在application.properties
中添加它@Autowired
private DataSource primaryDataSource;
@Autowired
@Qualifier("anotherDataSource")
private DataSource secondaryDataSource;
如果您注意到上述声明名称与配置类主要和次要中的数据源名称类似[请参阅@ConfigurationProperties前缀]
根据评论进行编辑....我希望这会有所帮助
spring.datasource.primary.url=jdbc:db2://host:50000/DEV1
spring.datasource.primary.username=dev1
spring.datasource.primary.password=password
spring.datasource.primary.driver-class-name=com.ibm.db2.jcc.DB2Driver
spring.datasource.secondary.url=jdbc:db2://host:50000/DEV2
spring.datasource.secondary.username=dev2
spring.datasource.secondary.password=password
spring.datasource.primary.driver-class-name=com.ibm.db2.jcc.DB2Driver