jSqlBox 3.0.0 release, comes with a distributed transaction Java ORM tool

jSqlBox3.0.0 release

jSqlBox is built on a core Java DbUtils full-featured database persistence layer tool, with cross-database (more than 80 dialects), DD L generation, pagination, multiple SQL writing, sub-library sub-table, declarative transactions, distributed transactions , master-slave, entity CURD, an entity associated with the query, ActiveRecord, Sql templates and other functions.

The main contents of this update: sub-library sub-table support distributed transactions

Gtx jSqlBox3.0.0 new transaction is a distributed transaction module, and its general idea Seata similar, but also automatically by a reverse roll back the recording, reduced invasion of the business, but the idea is to use jSqlBox distributed transaction built on ORM tool, not a content analysis of SQL, but the record is inserted entities, delete, modify operation to generate a rollback record. As a result, it is difficult to achieve on a lower level, at the expense of SQL support to achieve the best cross-database compatibility, support for all databases. In the specific implementation, it is by using the maximum guarantee complete model combines global record locking scheme, see the architecture  Bag Distributed Transaction improvements SAGA distributed transaction article.
image

The following is the configuration and presentation of a distributed transaction:

public class GtxTest {
	SqlBoxContext[] ctx = new SqlBoxContext[3];

	private static DataSource newTestDataSource() {
		HikariDataSource ds = new HikariDataSource();
		ds.setDriverClassName("org.h2.Driver");
		ds.setJdbcUrl("jdbc:h2:mem:" + new Random().nextLong() // random h2 ds name
				+ ";MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0");
		ds.setUsername("sa");
		ds.setPassword("");
		return ds;
	}

	@Before
	public void init() {
		SqlBoxContext lock = new SqlBoxContext(newTestDataSource());
		lock.setName("lock");
		lock.executeDDL(lock.toCreateDDL(GtxId.class));
		lock.executeDDL(lock.toCreateDDL(GtxLock.class));
		lock.executeDDL(lock.toCreateGtxLogDDL(Usr.class));
		GtxConnectionManager lockCM = new GtxConnectionManager(lock);
		for (int i = 0; i < 3; i++) {
			ctx[i] = new SqlBoxContext(newTestDataSource());
			ctx[i].setName("db");
			ctx[i].setDbCode(i);
			ctx[i].setConnectionManager(lockCM);
			ctx[i].setMasters(ctx);
			ctx[i].executeDDL(ctx[i].toCreateDDL(GtxTag.class));
			ctx[i].executeDDL(ctx[i].toCreateDDL(Usr.class));
		}
	}

	public void Div0Test() {
		ctx[0].startTrans();
		try {
			new Usr().insert(ctx[0]);
			new Usr().insert(ctx[1]);
			new Usr().insert(ctx[1]);
			new Usr().insert(ctx[2]);
			System.out.println(1 / 0);//强制出错
			ctx[0].commitTrans();
		} catch (Exception e) {
			TxResult result=ctx[0].rollbackTrans();
			GtxUnlockServ.forceUnlock(ctx[0], result);
		}
		Assert.assertEquals(0, ctx[0].eCountAll(Usr.class));
		Assert.assertEquals(0, ctx[1].eCountAll(Usr.class));
		Assert.assertEquals(0, ctx[2].eCountAll(Usr.class));
	}
}

The above example is the simplest demonstration of a distributed transaction that contains the data source configuration and build DDL table. If an error GTX transactions in the transaction is committed, regardless of whether part of the transaction commit occurs, as long as the database is not down machine, cable interrupted at any time, final data consistency is guaranteed, unlike XA protocol there may be data inconsistencies occur.

EXAMPLE upper GtxUnlockServ.forceUnlock (ctx [0], result) method only be used for testing, the actual project this line should be removed, and replaced with GtxUnlockServ.start (ctx, loopInterval, maxLoopQty); open a separate service unlocking method the second parameter is unlocked interval, in seconds, the timeout must be set to a value much greater than the time of the database transaction, the third parameter can be set to 0, indicating no maximum number of unlocking limit.

jSqlBox distributed transaction support sub-libraries, sub-table, and specify the number of lock server, if there is no such a high-performance (cloud) lock server, transaction processing performance can be improved by arranging a lock server cluster, of course, in this case, Specifies the number lock server is usually associated with traffic, such as red transfer distributed transaction, the envelopes can modulo ID number as a lock server. Specific examples please refer GtxShardDbTbLockDbTest.java and the cell under the test directory wiki

JSqlBox idea of ​​distributed transactions and almost simultaneously raised when Seata project open source, but from the point of view of almost complete, it does not mean I is high, a man turned dry Seata, but because of integration with ORM tools into one distributed Affairs, on the difficulty of writing this analysis is much smaller than Seata SQl syntax, from third-party tools underlying proxy data sources, and jSqlBox distributed transaction DataSource only supports a direct connection and does not provide call interface micro services.

The update also includes the following: 
1. demonstrates the use of Beetl as SQL template in the demo directory. The following example is a call Beetl template used to quickly locate the template text by using the Text class binding, you can easily use IDE features: 
List <the Map <String, Object >> USRs = ctx.tQueryForMapList (SelectUsers1.class, the bind ( "age", 50, " name", null));

2. JAVA8 class introduced $, a $, c $ simplified static wording, for example, Dao-benchmark program, the following SQL that was written to support the real font name remodeling:
List <demouser> Result = gctx () eFindBySample. (sample, "or", $ (u :: getCode), "? =", param ( "efg"));
where $ (u :: getCode) static method returns the "code" of this entity attribute name, also column names of the database (usually named after the agreement database column name and entity attributes consistent)

3. JDBPRO nonull the new method, like for non-empty query is determined, using the example:
"? And name like" ctx.iQueryForMapList ( "SELECT * WHERE from A =. 1. 1", nonull (, "%", name, "%"));

4. Other improvements:
The TinyTx classes are no longer used, renamed TinyTxAOP, jDialects ColumnModel class module length field is deleted, and correcting errors in the precision and scale set float.

5. In addition the following sub-projects related to the jSqlBox also be updated to version 3.0.0, will no longer be issued for updates, and the following sub-projects:
jDialects supports more than 80 database page, the type of transformation, DDL generation, entity Source generated database dialect tool 
jTransactions a separate transaction tools, including a distributed transaction capabilities
jDbPro this is jSqlBox kernel, built DbUtils based on SQL function only, does not include features such as ORM
MyFat Mybatis this is a plug-in that jSqlBox MyBatis and bundled by jSqlBox to complement missing entity Mybatis CRUD etc.

Expectations | Futures

Welcome to come up with better hair issue opinions or submit a PR, to help improve jSqlBox

Copyright | License

Apache 2.0

Guess you like

Origin www.oschina.net/news/109632/jsqlbox-3-0-0-released