/
MaihamaDBMasterSlaveManager.java
119 lines (110 loc) · 5.01 KB
/
MaihamaDBMasterSlaveManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2015-2022 the original author or authors.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.docksidestage.bizfw.masterslave.maihamadb.backstage;
import org.docksidestage.bizfw.masterslave.maihamadb.MaihamaSlaveDBAccessor;
import org.docksidestage.bizfw.masterslave.slavebasis.SlaveBasisAgent;
import org.docksidestage.bizfw.masterslave.slavebasis.ondestyle.SlaveBasisOnDemandAgent;
import org.docksidestage.dbflute.allcommon.DBCurrent;
import org.lastaflute.db.replication.selectable.SelectableDataSourceHolder;
import org.lastaflute.db.replication.slavedb.SlaveDBAccessor;
import org.lastaflute.web.ruts.process.ActionRuntime;
/**
* The manager of master/slave for the DB schema. <br>
* this schema uses slave-basis way.
* <pre>
* how to set up:
* 1. register this class to DI xml as DI component.
* app.xml
* |-dbflute.xml
* |-rdb.xml
* |-jdbc+.xml // here (if main schema)
* |-jdbc-[schema]-master.xml
* |-jdbc-[schema]-slave.xml
* or
* app.xml
* |-dbflute-[schema].xml
* |-rdb-[schema].xml
* |-jdbc-[schema].xml // here (if sub schema)
* |-jdbc-[schema]-master.xml
* |-jdbc-[schema]-slave.xml
*
* 2. call this class in action hook of BaseAction.
*
* @Resource
* private [Schema]MasterSlaveManager [schema]MasterSlaveManager;
*
* @Override
* public ActionResponse hookBefore(ActionRuntime runtime) {
* [schema]MasterSlaveManager.beginSlaveBasis();
* }
*
* @Override
* public void hookFinally(ActionRuntime runtime) {
* [schema]MasterSlaveManager.endSlaveBasis(runtime);
* }
* </pre>
* @author jflute
*/
public class MaihamaDBMasterSlaveManager { // DI component
// ===================================================================================
// Attribute
// =========
private final SlaveBasisAgent agent; // not null
// ===================================================================================
// Constructor
// ===========
public MaihamaDBMasterSlaveManager(MaihamaSlaveDBAccessor slaveDBAccessor, SelectableDataSourceHolder selectableDataSourceHolder) { // specific point, are injected
agent = createAgent(slaveDBAccessor, selectableDataSourceHolder);
}
// -----------------------------------------------------
// Agent Factory
// -------------
private SlaveBasisAgent createAgent(SlaveDBAccessor slaveDBAccessor, SelectableDataSourceHolder selectableDataSourceHolder) {
// you can select annotation way or on-demand way or ... here
return new SlaveBasisOnDemandAgent(getDBFluteProjectName(), slaveDBAccessor, selectableDataSourceHolder);
}
private String getDBFluteProjectName() {
// _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// DON'T FORGET to change this DB current
// when you copy this class
// _/_/_/_/_/_/_/_/_/_/
return DBCurrent.getInstance().projectName(); // specific point
}
// if you use annotation style
//private Class<? extends Annotation> getAnnotationType() {
// return MaihamaMasterDB.class; // specific point
//}
// ===================================================================================
// Hook Before
// ===========
/**
* See the agent's javadoc for the details.
* @param runtime The runtime object of currently-requested action. (NotNull)
*/
public void beginSlaveBasis(ActionRuntime runtime) {
agent.beginSlaveBasis(runtime);
}
// ===================================================================================
// Hook Finally
// ============
/**
* See the agent's javadoc for the details.
* @param runtime The runtime object of currently-requested action. (NotNull)
*/
public void endSlaveBasis(ActionRuntime runtime) {
agent.endSlaveBasis(runtime);
}
}