guzz结合spring进行事务管理的测试程序

在原org.guzz.dao.UserDaoImpl类中加入以下测试方法,在外部程序中调用update222()方法即可,说明guzz的确支持spring事务,可混用JDBCTemplate,以及获取当前事务的Connection做一些自己的,相当的方便。


public void addAndUpdateFind() {
		User u = new User();
		u.setUserName("xx1");
		this.insert(u);
		System.out.println("add1, id: " + u.getId());

		User u1 = new User();
		u1.setId(u.getId());
		u1.setUserName("xx2");
		this.update(u1);
		System.out.println("upd1, id: " + u1.getId());
	}

	public User upd2() {
		SearchExpression se = SearchExpression.forClass(User.class);
		se.and(Terms.eq("userName", "xx2"));
		User u2 = (User) this.getWriteTemplate().exportReadAPI().findObject(se);
		// User u2 = this.findByUserName("xx2") ;
		System.out.println("find1");
		u2.setUserName("xx upd name");
		this.update(u2);
		System.out.println("upd2");
		return u2;
	}

	private void testConn(User u) throws SQLException {
		WriteTranSession session = this.getWriteTemplate()
				.getWriteTranSession();
		// CompiledSQL cs = null;
		Connection conn = session.getRWConnection();
		// session.createCompiledSQLBatcher(cs);
		System.out.println(conn.getAutoCommit());
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
			String sql = "update tb_user set username='test conn' where pk="
					+ u.getId();
			stmt.executeUpdate(sql);
			System.out.println("testConn finished");
		} finally {
			stmt.close();
		}
	}
	
	private void testJdbcTemplate(User u) throws SQLException {
		ReadonlyTranSession session = this.getWriteTemplate().exportReadAPI();
		//WriteTranSession session = this.getTransactionManager().openRWTran(false);
		JDBCTemplate jt = session.createJDBCTemplate(User.class, null);
		Statement stmt = null;
		try {
			stmt = jt.getNativeConnection().createStatement();
			String sql = "update tb_user set username='testJdbcTemplate' where pk="
					+ u.getId();
			stmt.executeUpdate(sql);
			System.out.println("testConn finished");
			//session.commit();
		} finally {
			stmt.close();
			//session.close();
		}		
	}

	public void update222() {
		addAndUpdateFind();
		User u = upd2();
		try {
			testConn(u);
			testJdbcTemplate(u);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


你可能感兴趣的:(spring,guzz)