ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Giovanni Cuccu" <giovanni.cu...@gmail.com>
Subject How to use batch with IBatis
Date Mon, 12 May 2008 13:56:48 GMT
Hi,
   I've read the iBaits documentation, but I'm still unable to use batch api.
I tried to the following example in order to better understand how to
get started.
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;


public class TestIBatisBatch {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
	
		String filename="sql-map-config.xml";
		Reader reader=Resources.getResourceAsReader(filename);
		SqlMapClient sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader);
		
		try {
		sqlMap.startTransaction();
		sqlMap.startBatch();
		Map map=new HashMap();
		map.put("id", 1L);
		map.put("name", "test 1");
		sqlMap.insert("testiBatis.test_insert",map);
		map.clear();
		map.put("id", 2L);
		map.put("name", "test 2");
		sqlMap.insert("testiBatis.test_insert",map);
		map.clear();
		map.put("id", 3L);
		map.put("name", "test 3");
		sqlMap.insert("testiBatis.test_insert",map);
		int rows=sqlMap.executeBatch();
		System.out.println("inserite " + rows + " righe");
		sqlMap.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			sqlMap.endTransaction();
		}
		
		
	}

}
here are the relevant config files
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
    "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<settings useStatementNamespaces="true"/>
	
	
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
			<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/>
			<property name="JDBC.ConnectionURL"
value="jdbc:oracle:thin:@localhost:1521:orcl"/>
			<property name="JDBC.Password" value="test"/>
			<property name="JDBC.Username" value="test"/>
			<property name="JDBC.DefaultAutoCommit" value="false"/>
		</dataSource>
	</transactionManager>
  	
	<sqlMap resource="testiBatis.xml"/>
</sqlMapConfig>

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="testiBatis">
	
	<insert id="test_insert" parameterClass="java.util.Map">
		insert into modules(id,name) values(#id#,#name#)
	</insert>	
	
</sqlMap>


the preceding example insert three rows but I got the message
inserite 0 righe
My guess is than batching is not taking place but what I'm missing?
thanks,
    Giovanni


-- 
--------------------------------------------------------------------
"You don't know the power of dark side" - Darth Vader

Mime
View raw message