Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec1a116
commit e77a2e8
Showing
9 changed files
with
224 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
68 changes: 68 additions & 0 deletions
68
lealone-storage/hbase/src/main/java/org/lealone/hbase/command/router/MasterSlaveRouter.java
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,68 @@ | |||
/* | |||
* Licensed to the Apache Software Foundation (ASF) under one | |||
* or more contributor license agreements. See the NOTICE file | |||
* distributed with this work for additional information | |||
* regarding copyright ownership. The ASF licenses this file | |||
* to you 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.lealone.hbase.command.router; | |||
|
|||
import org.lealone.command.ddl.DefineCommand; | |||
import org.lealone.command.dml.Delete; | |||
import org.lealone.command.dml.Insert; | |||
import org.lealone.command.dml.Select; | |||
import org.lealone.command.dml.Update; | |||
import org.lealone.command.router.Router; | |||
import org.lealone.result.ResultInterface; | |||
|
|||
public class MasterSlaveRouter implements Router { | |||
private static final MasterSlaveRouter INSTANCE = new MasterSlaveRouter(); | |||
|
|||
public static MasterSlaveRouter getInstance() { | |||
return INSTANCE; | |||
} | |||
|
|||
private MasterSlaveRouter() { | |||
} | |||
|
|||
@Override | |||
public int executeDefineCommand(DefineCommand defineCommand) { | |||
// TODO Auto-generated method stub | |||
return 0; | |||
} | |||
|
|||
@Override | |||
public int executeInsert(Insert insert) { | |||
// TODO Auto-generated method stub | |||
return 0; | |||
} | |||
|
|||
@Override | |||
public int executeDelete(Delete delete) { | |||
// TODO Auto-generated method stub | |||
return 0; | |||
} | |||
|
|||
@Override | |||
public int executeUpdate(Update update) { | |||
// TODO Auto-generated method stub | |||
return 0; | |||
} | |||
|
|||
@Override | |||
public ResultInterface executeSelect(Select select, int maxRows, boolean scrollable) { | |||
// TODO Auto-generated method stub | |||
return null; | |||
} | |||
|
|||
} |
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
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
133 changes: 133 additions & 0 deletions
133
lealone-transaction/src/main/java/org/lealone/transaction/TransactionalRouter.java
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,133 @@ | |||
/* | |||
* Licensed to the Apache Software Foundation (ASF) under one | |||
* or more contributor license agreements. See the NOTICE file | |||
* distributed with this work for additional information | |||
* regarding copyright ownership. The ASF licenses this file | |||
* to you 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.lealone.transaction; | |||
|
|||
import org.lealone.command.ddl.DefineCommand; | |||
import org.lealone.command.dml.Delete; | |||
import org.lealone.command.dml.Insert; | |||
import org.lealone.command.dml.Select; | |||
import org.lealone.command.dml.TransactionCommand; | |||
import org.lealone.command.dml.Update; | |||
import org.lealone.command.router.Router; | |||
import org.lealone.engine.Session; | |||
import org.lealone.message.DbException; | |||
import org.lealone.result.ResultInterface; | |||
|
|||
public class TransactionalRouter implements Router { | |||
private final Router nestedRouter; | |||
|
|||
public TransactionalRouter(Router nestedRouter) { | |||
this.nestedRouter = nestedRouter; | |||
} | |||
|
|||
@Override | |||
public int executeDefineCommand(DefineCommand defineCommand) { | |||
return nestedRouter.executeDefineCommand(defineCommand); | |||
} | |||
|
|||
@Override | |||
public int executeInsert(Insert insert) { | |||
boolean isTopTransaction = false; | |||
boolean isNestedTransaction = false; | |||
Session session = insert.getSession(); | |||
try { | |||
if (insert.isBatch()) { | |||
if (session.getAutoCommit()) { | |||
session.setAutoCommit(false); | |||
isTopTransaction = true; | |||
} else { | |||
isNestedTransaction = true; | |||
session.addSavepoint(TransactionCommand.INTERNAL_SAVEPOINT); | |||
} | |||
} | |||
int updateCount = nestedRouter.executeInsert(insert); | |||
if (isTopTransaction) | |||
session.commit(false); | |||
return updateCount; | |||
} catch (Exception e) { | |||
if (isTopTransaction) | |||
session.rollback(); | |||
|
|||
//嵌套事务出错时提前rollback | |||
if (isNestedTransaction) | |||
session.rollbackToSavepoint(TransactionCommand.INTERNAL_SAVEPOINT); | |||
|
|||
throw DbException.convert(e); | |||
} finally { | |||
if (isTopTransaction) | |||
session.setAutoCommit(true); | |||
} | |||
} | |||
|
|||
@Override | |||
public int executeDelete(Delete delete) { | |||
return executeUpdateOrDelete(null, delete); | |||
} | |||
|
|||
@Override | |||
public int executeUpdate(Update update) { | |||
return executeUpdateOrDelete(update, null); | |||
} | |||
|
|||
private int executeUpdateOrDelete(Update update, Delete delete) { | |||
boolean isTopTransaction = false; | |||
boolean isNestedTransaction = false; | |||
|
|||
Session session; | |||
if (update != null) | |||
session = update.getSession(); | |||
else | |||
session = delete.getSession(); | |||
try { | |||
if (session.getAutoCommit()) { | |||
session.setAutoCommit(false); | |||
isTopTransaction = true; | |||
} else { | |||
isNestedTransaction = true; | |||
session.addSavepoint(TransactionCommand.INTERNAL_SAVEPOINT); | |||
} | |||
int updateCount; | |||
if (update != null) | |||
updateCount = nestedRouter.executeUpdate(update); | |||
else | |||
updateCount = nestedRouter.executeDelete(delete); | |||
if (isTopTransaction) | |||
session.commit(false); | |||
return updateCount; | |||
} catch (Exception e) { | |||
if (isTopTransaction) | |||
session.rollback(); | |||
|
|||
//嵌套事务出错时提前rollback | |||
if (isNestedTransaction) | |||
session.rollbackToSavepoint(TransactionCommand.INTERNAL_SAVEPOINT); | |||
|
|||
throw DbException.convert(e); | |||
} finally { | |||
if (isTopTransaction) | |||
session.setAutoCommit(true); | |||
} | |||
|
|||
} | |||
|
|||
@Override | |||
public ResultInterface executeSelect(Select select, int maxRows, boolean scrollable) { | |||
return nestedRouter.executeSelect(select, maxRows, scrollable); | |||
} | |||
|
|||
} |