-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Execute Sql file in NHibernate
PostPosted: Tue Apr 11, 2006 4:57 pm 
Newbie

Joined: Mon Jan 02, 2006 9:25 pm
Posts: 3
Hello,

I have a SQL file with some queries to create and modify tables, to insert data, etc... in the same transaction that I execute this script file I make alterations in some objects persisted for the NHibernate. The question is: how to execute this SQL file in the same transaction that the NHibernate uses?

Obs: the tables that I create and modify do not have nothing to do with objects mapped for the NHibernate.

[]'s

Rodrigo C. A.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 7:26 pm 
Beginner
Beginner

Joined: Mon Sep 26, 2005 5:57 pm
Posts: 39
Have you tried Session.Connection.CreateCommand()?

You can start a new transaction by calling Session.BeginTransaction, and after that, when use Session.Connection.CreateCommand() to execute your sql script, that script will get executed on the same transaction.

BTW, ddl changes like table structure modifications are not transactional.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 9:57 am 
Newbie

Joined: Mon Jan 02, 2006 9:25 pm
Posts: 3
Yes, I tried this:
Code:
ITransaction trans = this.GetSession().BeginTransaction();
IDbCommand command = this.GetSession().Connection.CreateCommand();
command.CommandText = sql;
command.ExecuteNonQuery();
trans.Commit();

But I get this:
Code:
[InvalidOperationException: ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized.]
   System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +873221
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +72
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
   ...


[]'s

Rodrigo C. A.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 10:05 am 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
You forgot to assign the transaction to the command.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 10:19 am 
Newbie

Joined: Mon Jan 02, 2006 9:25 pm
Posts: 3
But how I do this? I need the command uses same transaction of the NHibernate.

I tried use another transaction, but if I try to create another transaction I get:

Code:
SqlConnection does not support parallel transactions.

Because I already had initiated the transaction of the NHibernate before.

I tried this too:
Code:
ITransaction trans = this.GetSession().BeginTransaction();
IQuery query = this.GetSession().CreateSQLQuery(sql, returnAlias, returnClass);
query.List();
this.GetSession().SaveOrUpdate(obj1);
this.GetSession().SaveOrUpdate(obj2);
trans.Commit();

But I get:
Code:
SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.

In this way the sql script executes, but the alterations that I make in objects not.

[]'s

Rodrigo C. A.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 1:39 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
Code:
ITransaction trans = this.GetSession().BeginTransaction();
IDbCommand command = this.GetSession().Connection.CreateCommand();
command.CommandText = sql;
command.Transaction = trans; // <== add this line
command.ExecuteNonQuery();
trans.Commit();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 4:39 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Actually, the added line should be:
Code:
trans.Enlist(command);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 6:30 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
Oops. Yeah, I was thinking of IDbTransaction.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.