I did a simple test.
My hbm.xml is
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="h2test.test.Book" table="Book">
<id column="book_id" name="id" type="java.lang.Long">
<generator class="identity"/>
</id>
<property column="book_name" length="50" name="bookName" type="java.lang.String"/>
<property column="put_date" length="23" name="putDate" type="java.util.Date"/>
<property column="price" length="19" name="price" type="java.lang.Double"/>
<property column="discount_id" length="10" name="discountId" type="java.lang.Integer"/>
</class>
</hibernate-mapping>
Insert code:
Code:
private void testInsert() throws Exception
{
List list = new ArrayList();
double price = Math.random() * 100;
Book book = new Book();
book.setPrice(new Double(price));
list.add(book);
price = Math.random() * 100;
book = new Book();
book.setPrice(new Double(price));
list.add(book);
price = Math.random() * 100;
book = new Book();
book.setPrice(new Double(price));
list.add(book);//If I remove this line, it works well.Insert two records into database.
new BookDAO().add(list);
}
Code:
public void add(List obj) throws HibernateException
{
Session session = HibernateUtil.getSession();
Transaction transcation = HibernateUtil.beginTransaction();
for (int i = 0; i < obj.size(); i++){
session.save(obj.get(i));
session.flush();
}
HibernateUtil.commitTransaction();
}
Exception is:
Code:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
Full hibernate log is here:
Code:
2004-02-26 17:18:20,850(2250) [main] DEBUG net.sf.hibernate.impl.SessionImpl - opened session
2004-02-26 17:18:20,850(2250) [main] DEBUG net.sf.hibernate.transaction.JDBCTransaction - begin
2004-02-26 17:18:20,850(2250) [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - total checked-out connections: 0
2004-02-26 17:18:20,850(2250) [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
2004-02-26 17:18:20,881(2281) [main] DEBUG net.sf.hibernate.transaction.JDBCTransaction - current autocommit status:false
2004-02-26 17:18:20,881(2281) [main] DEBUG net.sf.hibernate.impl.SessionImpl - saving [h2test.test.Book#<null>]
2004-02-26 17:18:20,881(2281) [main] DEBUG net.sf.hibernate.impl.SessionImpl - executing insertions
2004-02-26 17:18:20,881(2281) [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: h2test.test.Book (native id)
2004-02-26 17:18:20,897(2297) [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-02-26 17:18:20,897(2297) [main] DEBUG net.sf.hibernate.SQL - insert into Book (book_name, put_date, price, discount_id) values (?, ?, ?, ?) select SCOPE_IDENTITY()
Hibernate: insert into Book (book_name, put_date, price, discount_id) values (?, ?, ?, ?) select SCOPE_IDENTITY()
2004-02-26 17:18:20,912(2312) [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2004-02-26 17:18:21,022(2422) [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [h2test.test.Book#<null>]
2004-02-26 17:18:21,022(2422) [main] DEBUG net.sf.hibernate.type.StringType - binding null to parameter: 1
2004-02-26 17:18:21,053(2453) [main] DEBUG net.sf.hibernate.type.TimestampType - binding null to parameter: 2
2004-02-26 17:18:21,053(2453) [main] DEBUG net.sf.hibernate.type.DoubleType - binding '47.97431365015803' to parameter: 3
2004-02-26 17:18:21,053(2453) [main] DEBUG net.sf.hibernate.type.IntegerType - binding null to parameter: 4
2004-02-26 17:18:21,084(2484) [main] DEBUG net.sf.hibernate.persister.AbstractEntityPersister - Natively generated identity: 12
2004-02-26 17:18:21,084(2484) [main] DEBUG net.sf.hibernate.impl.SessionImpl - flushing session
2004-02-26 17:18:21,084(2484) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Flushing entities and processing referenced collections
2004-02-26 17:18:21,084(2484) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Processing unreferenced collections
2004-02-26 17:18:21,084(2484) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Scheduling collection removes/(re)creates/updates
2004-02-26 17:18:21,084(2484) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2004-02-26 17:18:21,084(2484) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.impl.Printer - listing entities:
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.impl.Printer - h2test.test.Book{price=47.97431365015803, discountId=null, putDate=null, id=12, bookName=null}
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.impl.SessionImpl - executing flush
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.impl.SessionImpl - post flush
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.impl.SessionImpl - saving [h2test.test.Book#<null>]
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.impl.SessionImpl - executing insertions
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: h2test.test.Book (native id)
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 1 open PreparedStatements, 0 open ResultSets
2004-02-26 17:18:21,100(2500) [main] DEBUG net.sf.hibernate.SQL - insert into Book (book_name, put_date, price, discount_id) values (?, ?, ?, ?) select SCOPE_IDENTITY()
Hibernate: insert into Book (book_name, put_date, price, discount_id) values (?, ?, ?, ?) select SCOPE_IDENTITY()
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [h2test.test.Book#<null>]
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.type.StringType - binding null to parameter: 1
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.type.TimestampType - binding null to parameter: 2
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.type.DoubleType - binding '40.0630124059981' to parameter: 3
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.type.IntegerType - binding null to parameter: 4
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.persister.AbstractEntityPersister - Natively generated identity: 13
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.impl.SessionImpl - flushing session
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Flushing entities and processing referenced collections
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Processing unreferenced collections
2004-02-26 17:18:21,115(2515) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Scheduling collection removes/(re)creates/updates
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.Printer - listing entities:
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.Printer - h2test.test.Book{price=47.97431365015803, discountId=null, putDate=null, id=12, bookName=null}
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.Printer - h2test.test.Book{price=40.0630124059981, discountId=null, putDate=null, id=13, bookName=null}
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.SessionImpl - executing flush
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.SessionImpl - post flush
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.SessionImpl - saving [h2test.test.Book#<null>]
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.SessionImpl - executing insertions
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: h2test.test.Book (native id)
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 2 open PreparedStatements, 0 open ResultSets
2004-02-26 17:18:21,131(2531) [main] DEBUG net.sf.hibernate.SQL - insert into Book (book_name, put_date, price, discount_id) values (?, ?, ?, ?) select SCOPE_IDENTITY()
Hibernate: insert into Book (book_name, put_date, price, discount_id) values (?, ?, ?, ?) select SCOPE_IDENTITY()
2004-02-26 17:18:21,147(2547) [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2004-02-26 17:18:21,147(2547) [main] DEBUG net.sf.hibernate.util.JDBCExceptionReporter - SQL Exception
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.setupImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.<init>(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.<init>(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.prepareStatement(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.prepareStatement(Unknown Source)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:249)
at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:61)
at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:56)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:505)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:906)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:839)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:757)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720)
at h2test.test.BookDAO.add(BookDAO.java:27)
at h2test.test.H2Example.testInsert(H2Example.java:29)
at h2test.test.H2Example.main(H2Example.java:35)
I don't know how hibernate call jdbc driver. Is it the problem in MSSQL jdbc driver? I use MSSQL jdbc driver provided by MSSQL. Is there any good driver to use?