📄 transactions.po
字号:
"}\n""catch (RuntimeException e) {\n"" factory.getCurrentSession().getTransaction().rollback();\n"" throw e; // or display error message\n""}]]>"msgstr ""#. Tag: para#: transactions.xml:464#, no-c-formatmsgid """You will very likely never see these code snippets in a regular application; ""fatal (system) exceptions should always be caught at the \"top\". In other ""words, the code that executes Hibernate calls (in the persistence layer) and ""the code that handles <literal>RuntimeException</literal> (and usually can ""only clean up and exit) are in different layers. The current context ""management by Hibernate can significantly simplify this design, as all you ""need is access to a <literal>SessionFactory</literal>. Exception handling is ""discussed later in this chapter."msgstr """你很可能从未在一个通常的应用程序的业务代码中见过这样的代码片断:致命的(系""统)异常应该总是 在应用程序“顶层”被捕获。换句话说,执行Hibernate调用的代码""(在持久层)和处理 <literal>RuntimeException</literal>异常的代码(通常只能清""理和退出应用程序)应该在不同 的应用程序逻辑层。Hibernate的当前上下文管理可以""极大地简化这一设计,你所有的一切就是<literal>SessionFactory</literal>。 异常""处理将在本章稍后进行讨论。"#. Tag: para#: transactions.xml:474#, no-c-formatmsgid """Note that you should select <literal>org.hibernate.transaction.""JDBCTransactionFactory</literal> (which is the default), and for the second ""example <literal>\"thread\"</literal> as your <literal>hibernate.""current_session_context_class</literal>."msgstr """请注意,你应该选择 <literal>org.hibernate.transaction.""JDBCTransactionFactory</literal> (这是默认选项),对第二个例子来说,""<literal>hibernate.current_session_context_class</literal>应该是<literal>""\"thread\"</literal>"#. Tag: title#: transactions.xml:483#, no-c-formatmsgid "Using JTA"msgstr "使用JTA"#. Tag: para#: transactions.xml:485#, no-c-formatmsgid """If your persistence layer runs in an application server (e.g. behind EJB ""session beans), every datasource connection obtained by Hibernate will ""automatically be part of the global JTA transaction. You can also install a ""standalone JTA implementation and use it without EJB. Hibernate offers two ""strategies for JTA integration."msgstr """如果你的持久层运行在一个应用服务器中(例如,在EJB session beans的后面),""Hibernate获取 的每个数据源连接将自动成为全局JTA事务的一部分。 你可以安装一个""独立的JTA实现,使用它而不使用EJB。Hibernate提供了两种策略进行JTA集成。"#. Tag: para#: transactions.xml:492#, no-c-formatmsgid """If you use bean-managed transactions (BMT) Hibernate will tell the ""application server to start and end a BMT transaction if you use the ""<literal>Transaction</literal> API. So, the transaction management code is ""identical to the non-managed environment."msgstr """如果你使用bean管理事务(BMT),可以通过使用Hibernate的 <literal>Transaction</""literal> API来告诉 应用服务器启动和结束BMT事务。因此,事务管理代码和在非托管""环境下是一样的。"#. Tag: programlisting#: transactions.xml:498#, no-c-formatmsgid """<![CDATA[// BMT idiom\n""Session sess = factory.openSession();\n""Transaction tx = null;\n""try {\n"" tx = sess.beginTransaction();\n""\n"" // do some work\n"" ...\n""\n"" tx.commit();\n""}\n""catch (RuntimeException e) {\n"" if (tx != null) tx.rollback();\n"" throw e; // or display error message\n""}\n""finally {\n"" sess.close();\n""}]]>"msgstr ""#. Tag: para#: transactions.xml:500#, no-c-formatmsgid """If you want to use a transaction-bound <literal>Session</literal>, that is, ""the <literal>getCurrentSession()</literal> functionality for easy context ""propagation, you will have to use the JTA <literal>UserTransaction</literal> ""API directly:"msgstr """如果你希望使用与事务绑定的<literal>Session</literal>,也就是使用""<literal>getCurrentSession()</literal>来简化上下文管理,你将不得不直接使用""JTA <literal>UserTransaction</literal>API。"#. Tag: programlisting#: transactions.xml:506#, no-c-formatmsgid """<![CDATA[// BMT idiom with getCurrentSession()\n""try {\n"" UserTransaction tx = (UserTransaction)new InitialContext()\n"" .lookup(\"java:comp/UserTransaction\");\n""\n"" tx.begin();\n""\n"" // Do some work on Session bound to transaction\n"" factory.getCurrentSession().load(...);\n"" factory.getCurrentSession().persist(...);\n""\n"" tx.commit();\n""}\n""catch (RuntimeException e) {\n"" tx.rollback();\n"" throw e; // or display error message\n""}]]>"msgstr ""#. Tag: para#: transactions.xml:508#, fuzzy, no-c-formatmsgid """With CMT, transaction demarcation is done in session bean deployment ""descriptors, not programmatically, hence, the code is reduced to:"msgstr """在CMT方式下,事务声明是在session bean的部署描述符中,而不需要编程。 因此,代""码被简化为:"#. Tag: programlisting#: transactions.xml:513#, no-c-formatmsgid """<![CDATA[// CMT idiom\n"" Session sess = factory.getCurrentSession();\n""\n"" // do some work\n"" ...\n""]]>"msgstr ""#. Tag: para#: transactions.xml:515#, no-c-formatmsgid """In a CMT/EJB even rollback happens automatically, since an unhandled ""<literal>RuntimeException</literal> thrown by a session bean method tells ""the container to set the global transaction to rollback. <emphasis>This ""means you do not need to use the Hibernate <literal>Transaction</literal> ""API at all with BMT or CMT, and you get automatic propagation of the ""\"current\" Session bound to the transaction.</emphasis>"msgstr """在CMT/EJB中甚至会自动rollback,因为假若有未捕获的<literal>RuntimeException</""literal>从session bean方法中抛出,这就会通知容器把全局事务回滚。<emphasis>这""就意味着,在BMT或者CMT中,你根本就不需要使用Hibernate <literal>Transaction</""literal> API ,你自动得到了绑定到事务的“当前”Session。 </emphasis>"#. Tag: para#: transactions.xml:523#, fuzzy, no-c-formatmsgid """Note that you should choose <literal>org.hibernate.transaction.""JTATransactionFactory</literal> if you use JTA directly (BMT), and ""<literal>org.hibernate.transaction.CMTTransactionFactory</literal> in a CMT ""session bean, when you configure Hibernate's transaction factory. Remember ""to also set <literal>hibernate.transaction.manager_lookup_class</literal>. ""Furthermore, make sure that your <literal>hibernate.""current_session_context_class</literal> is either unset (backwards ""compatibility), or set to <literal>\"jta\"</literal>."msgstr """注意,当你配置Hibernate的transaction factory的时候,在直接使用JTA的时候""(BMT),你应该选择<literal>org.hibernate.transaction.JTATransactionFactory</""literal>,在CMT session bean中选择<literal>org.hibernate.transaction.""CMTTransactionFactory</literal>。记得也要设置<literal>hibernate.transaction.""manager_lookup_class</literal>。还有,确认你的<literal>hibernate.""current_session_context_class</literal>未设置(为了向下兼容),或者设置为""<literal>\"jta\"</literal>。"#. Tag: para#: transactions.xml:532#, fuzzy, no-c-formatmsgid """The <literal>getCurrentSession()</literal> operation has one downside in a ""JTA environment. There is one caveat to the use of <literal>after_statement</""literal> connection release mode, which is then used by default. Due to a ""silly limitation of the JTA spec, it is not possible for Hibernate to ""automatically clean up any unclosed <literal>ScrollableResults</literal> or ""<literal>Iterator</literal> instances returned by <literal>scroll()</""literal> or <literal>iterate()</literal>. You <emphasis>must</emphasis> ""release the underlying database cursor by calling <literal>ScrollableResults.""close()</literal> or <literal>Hibernate.close(Iterator)</literal> explicitly ""from a <literal>finally</literal> block. (Of course, most applications can ""easily avoid using <literal>scroll()</literal> or <literal>iterate()</""literal> at all from the JTA or CMT code.)"msgstr """<literal>getCurrentSession()</literal>在JTA环境中有一个弊端。对""<literal>after_statement</literal>连接释放方式有一个警告,这是被默认使用的。""因为JTA规范的一个很愚蠢的限制,Hibernate不可能自动清理任何未关闭的""<literal>ScrollableResults</literal> 或者<literal>Iterator</literal>,它们是""由<literal>scroll()</literal>或<literal>iterate()</literal>产生的。你""<emphasis>must</emphasis>通过在<literal>finally</literal>块中,显式调用""<literal>ScrollableResults.close()</literal>或者<literal>Hibernate.close""(Iterator)</literal>方法来释放底层数据库游标。(当然,大部分程序完全可以很容易""的避免在JTA或CMT代码中出现<literal>scroll()</literal>或<literal>iterate()</""literal>。)"#. Tag: title#: transactions.xml:548#, no-c-formatmsgid "Exception handling"msgstr "异常处理"#. Tag: para#: transactions.xml:550#, no-c-formatmsgid """If the <literal>Session</literal> throws an exception (including any ""<literal>SQLException</literal>), you should immediately rollback the ""database transaction, call <literal>Session.close()</literal> and discard ""the <literal>Session</literal> instance. Certain methods of ""<literal>Session</literal> will <emphasis>not</emphasis> leave the session ""in a consistent state. No exception thrown by Hibernate can be treated as ""recoverable. Ensure that the <literal>Session</literal> will be closed by ""calling <literal>close()</literal> in a <literal>finally</literal> block."msgstr """如果 <literal>Session</literal> 抛出异常 (包括任何<literal>SQLException</""literal>), 你应该立即回滚数据库事务,调用 <literal>Session.close()</""literal> ,丢弃该 <literal>Session</literal>实例。<literal>Session</literal>""的某些方法可能会导致session 处于不一致的状态。所有由Hibernate抛出的异常都视为""不可以恢复的。确保在 <literal>finally</literal> 代码块中调用<literal>close()""</literal>方法,以关闭掉 <literal>Session</literal>。"#. Tag: para#: transactions.xml:561#, no-c-formatmsgid """The <literal>HibernateException</literal>, which wraps most of the errors ""that can occur in a Hibernate persistence layer, is an unchecked exception ""(it wasn't in older versions of Hibernate). In our opinion, we shouldn't ""force the application developer to catch an unrecoverable exception at a low ""layer. In most systems, unchecked and fatal exceptions are handled in one of ""the first frames of the method call stack (i.e. in higher layers) and an ""error message is presented to the application user (or some other ""appropriate action is taken). Note that Hibernate might also throw other ""unchecked exceptions which are not a <literal>HibernateException</literal>. ""These are, again, not recoverable and appropriate action should be taken."msgstr """<literal>HibernateException</literal>是一个非检查期异常(这不同于Hibernate老""的版本), 它封装了Hibernate持久层可能出现的大多数错误。我们的观点是,不应该""强迫应用程序开发人员 在底层捕获无法恢复的异常。在大多数软件系统中,非检查期异""常和致命异常都是在相应方法调用 的堆栈的顶层被处理的(也就是说,在软件上面的逻""辑层),并且提供一个错误信息给应用软件的用户 (或者采取其他某些相应的操作)。""请注意,Hibernate也有可能抛出其他并不属于 <literal>HibernateException</""literal>的非检查期异常。这些异常同样也是无法恢复的,应该 采取某些相应的操作去""处理。"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -