Tuesday, August 29, 2006

NHibernate and ORA-12571 Errors

I've been attempting to apply my Hibernate knowledge to some new C# development that I've been doing with NHibernate, and came into a bit of a crazy situation.

When I was working on the mapping of the first table (to make sure that all my connection setup infrastructure was working), I came upon what seemed like a particularly pernicious bug: ORA-12571 ("TNS:packet writer failure" message string) errors were occurring constantly when I did a query using a named parameter. Googling these errors seemed to imply that there might be some type of networking problem going on, so as I don't have much access to the development Oracle instance that I was using (Oracle 9.2.0.7 for the pedantic), I enlisted our Systems team.

Turns out that on the server we were seeing log messages of "ORA-00600" on the statement in question, indicating that there's corruption or an error in the format on the data being sent to Oracle by the client.

After doing some more experimentation, I came up with the actual issue, completely shrouded by all this networking gobbledigook: NLS. Or, specifically, the difference between DbType.String and DbType.AnsiString in ADO.NET.

In Hibernate, since Java only uses Unicode internally, I got quite used to just saying query.setString("Foo", val) rather than trying to actually figure out the differences between Java/JDBC and ADO.NET. Turns out that ADO.NET has a difference which flows through to NHibernate.

So, to make a long post short, if you run into ORA-12571 errors using parameters in NHibernate, it's probably not your network or machine, but check that you're using the right data binding in your query binding (e.g. query.SetAnsiString() rather than query.SetString()), because if your database is expecting a particular character encoding and you don't send it, it shows up as a network corruption message.
blog comments powered by Disqus