📄 transactions.po
字号:
#. Tag: para#: transactions.xml:322#, no-c-formatmsgid """Never use the anti-patterns <emphasis>session-per-user-session</emphasis> or ""<emphasis>session-per-application</emphasis> (of course, there are rare ""exceptions to this rule). Note that some of the following issues might also ""appear with the recommended patterns, make sure you understand the ""implications before making a design decision:"msgstr """决不要使用反模式<emphasis>session-per-user-session</emphasis>或者<emphasis> ""session-per-application</emphasis>(当然,这个规定几乎没有例外)。请注意, 下""述一些问题可能也会出现在我们推荐的模式中,在你作出某个设计决定之前,请务必理""解该模式的应用前提。"#. Tag: para#: transactions.xml:331#, no-c-formatmsgid """A <literal>Session</literal> is not thread-safe. Things which are supposed ""to work concurrently, like HTTP requests, session beans, or Swing workers, ""will cause race conditions if a <literal>Session</literal> instance would be ""shared. If you keep your Hibernate <literal>Session</literal> in your ""<literal>HttpSession</literal> (discussed later), you should consider ""synchronizing access to your Http session. Otherwise, a user that clicks ""reload fast enough may use the same <literal>Session</literal> in two ""concurrently running threads."msgstr """<literal>Session</literal> 对象是非线程安全的。如果一个<literal>Session</""literal> 实例允许共享的话,那些支持并发运行的东东,例如HTTP request,session ""beans,或者是 Swing workers,将会导致出现资源争用(race condition)。如果在""<literal>HttpSession</literal>中有 Hibernate 的<literal>Session</literal>的话""(稍后讨论),你应该考虑同步访问你的Http session。 否则,只要用户足够快的点击""浏览器的“刷新”,就会导致两个并发运行线程使用同一个 <literal>Session</""literal>。"#. Tag: para#: transactions.xml:342#, no-c-formatmsgid """An exception thrown by Hibernate means you have to rollback your database ""transaction and close the <literal>Session</literal> immediately (discussed ""later in more detail). If your <literal>Session</literal> is bound to the ""application, you have to stop the application. Rolling back the database ""transaction doesn't put your business objects back into the state they were ""at the start of the transaction. This means the database state and the ""business objects do get out of sync. Usually this is not a problem, because ""exceptions are not recoverable and you have to start over after rollback ""anyway."msgstr """一个由Hibernate抛出的异常意味着你必须立即回滚数据库事务,并立即关闭""<literal>Session</literal> (稍后会展开讨论)。如果你的<literal>Session</""literal>绑定到一个应用程序上,你必 须停止该应用程序。回滚数据库事务并不会把你""的业务对象退回到事务启动时候的状态。这 意味着数据库状态和业务对象状态不同步。""通常情况下,这不是什么问题,因为异常是不可 恢复的,你必须在回滚之后重新开始执""行。"#. Tag: para#: transactions.xml:354#, no-c-formatmsgid """The <literal>Session</literal> caches every object that is in persistent ""state (watched and checked for dirty state by Hibernate). This means it ""grows endlessly until you get an OutOfMemoryException, if you keep it open ""for a long time or simply load too much data. One solution for this is to ""call <literal>clear()</literal> and <literal>evict()</literal> to manage the ""<literal>Session</literal> cache, but you most likely should consider a ""Stored Procedure if you need mass data operations. Some solutions are shown ""in <xref linkend=\"batch\"/>. Keeping a <literal>Session</literal> open for ""the duration of a user session also means a high probability of stale data."msgstr """<literal>Session</literal> 缓存了处于持久化状态的每个对象(Hibernate会监视和""检查脏数据)。 这意味着,如果你让<literal>Session</literal>打开很长一段时间,""或是仅仅载入了过多的数据, <literal>Session</literal>占用的内存会一直增长,直""到抛出OutOfMemoryException异常。这个 问题的一个解决方法是调用<literal>clear()""</literal> 和<literal>evict()</literal>来管理 <literal>Session</literal>的缓""存,但是如果你需要大批量数据操作的话,最好考虑 使用存储过程。在<xref linkend=""\"batch\"/>中有一些解决方案。在用户会话期间一直保持 <literal>Session</""literal>打开也意味着出现脏数据的可能性很高。"#. Tag: title#: transactions.xml:372#, no-c-formatmsgid "Database transaction demarcation"msgstr "数据库事务声明"#. Tag: para#: transactions.xml:374#, fuzzy, no-c-formatmsgid """Database (or system) transaction boundaries are always necessary. No ""communication with the database can occur outside of a database transaction ""(this seems to confuse many developers who are used to the auto-commit ""mode). Always use clear transaction boundaries, even for read-only ""operations. Depending on your isolation level and database capabilities this ""might not be required but there is no downside if you always demarcate ""transactions explicitly. Certainly, a single database transaction is going ""to perform better than many small transactions, even for reading data."msgstr """数据库(或者系统)事务的声明总是必须的。在数据库事务之外,就无法和数据库通讯""(这可能会让那些习惯于 自动提交事务模式的开发人员感到迷惑)。永远使用清晰的事""务声明,即使只读操作也是如此。进行 显式的事务声明并不总是需要的,这取决于你的""事务隔离级别和数据库的能力,但不管怎么说,声明事务总归有益无害。当然,一个单""独的数据库事务总是比很多琐碎的事务性能更好,即时对读数据而言也是一样。"#. Tag: para#: transactions.xml:384#, no-c-formatmsgid """A Hibernate application can run in non-managed (i.e. standalone, simple Web- ""or Swing applications) and managed J2EE environments. In a non-managed ""environment, Hibernate is usually responsible for its own database ""connection pool. The application developer has to manually set transaction ""boundaries, in other words, begin, commit, or rollback database transactions ""himself. A managed environment usually provides container-managed ""transactions (CMT), with the transaction assembly defined declaratively in ""deployment descriptors of EJB session beans, for example. Programmatic ""transaction demarcation is then no longer necessary."msgstr """一个Hibernate应用程序可以运行在非托管环境中(也就是独立运行的应用程序,简单""Web应用程序, 或者Swing图形桌面应用程序),也可以运行在托管的J2EE环境中。在一""个非托管环境中,Hibernate 通常自己负责管理数据库连接池。应用程序开发人员必须""手工设置事务声明,换句话说,就是手工启 动,提交,或者回滚数据库事务。一个托管""的环境通常提供了容器管理事务(CMT),例如事务装配通过可声 明的方式定义在EJB ""session beans的部署描述符中。可编程式事务声明不再需要,即使是 ""<literal>Session</literal> 的同步也可以自动完成。"#. Tag: para#: transactions.xml:394#, fuzzy, no-c-formatmsgid """However, it is often desirable to keep your persistence layer portable ""between non-managed resource-local environments, and systems that can rely ""on JTA but use BMT instead of CMT. In both cases you'd use programmatic ""transaction demarcation. Hibernate offers a wrapper API called ""<literal>Transaction</literal> that translates into the native transaction ""system of your deployment environment. This API is actually optional, but we ""strongly encourage its use unless you are in a CMT session bean."msgstr """让持久层具备可移植性是人们的理想,这种移植发生在非托管的本地资源环境,与依赖""JTA但是使用BMT而非CMT的系统之间。在两种情况下你都可以使用编程式的事务管理。""Hibernate提供了一套称为<literal>Transaction</literal>的封装API, 用来把你的部""署环境中的本地事务管理系统转换到Hibernate事务上。这个API是可选的,但是我们强""烈 推荐你使用,除非你用CMT session bean。"#. Tag: para#: transactions.xml:403#, no-c-formatmsgid """Usually, ending a <literal>Session</literal> involves four distinct phases:"msgstr "通常情况下,结束 <literal>Session</literal> 包含了四个不同的阶段:"#. Tag: para#: transactions.xml:409#, no-c-formatmsgid "flush the session"msgstr "同步session(flush,刷出到磁盘)"#. Tag: para#: transactions.xml:414#, no-c-formatmsgid "commit the transaction"msgstr "提交事务"#. Tag: para#: transactions.xml:419#, no-c-formatmsgid "close the session"msgstr "关闭session"#. Tag: para#: transactions.xml:424#, no-c-formatmsgid "handle exceptions"msgstr "处理异常"#. Tag: para#: transactions.xml:430#, no-c-formatmsgid """Flushing the session has been discussed earlier, we'll now have a closer ""look at transaction demarcation and exception handling in both managed- and ""non-managed environments."msgstr """session的同步(flush,刷出)前面已经讨论过了,我们现在进一步考察在托管和非托管""环境下的事务声明和异常处理。"#. Tag: title#: transactions.xml:437#, no-c-formatmsgid "Non-managed environment"msgstr "非托管环境"#. Tag: para#: transactions.xml:439#, no-c-formatmsgid """If a Hibernate persistence layer runs in a non-managed environment, database ""connections are usually handled by simple (i.e. non-DataSource) connection ""pools from which Hibernate obtains connections as needed. The session/""transaction handling idiom looks like this:"msgstr """如果Hibernat持久层运行在一个非托管环境中,数据库连接通常由Hibernate的简单(即""非DataSource)连接池机制 来处理。session/transaction处理方式如下所示:"#. Tag: programlisting#: transactions.xml:446#, no-c-formatmsgid """<![CDATA[// Non-managed environment 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:448#, fuzzy, no-c-formatmsgid """You don't have to <literal>flush()</literal> the <literal>Session</literal> ""explicitly - the call to <literal>commit()</literal> automatically triggers ""the synchronization (depending upon the <link linkend=\"objectstate-flushing""\">FlushMode</link> for the session. A call to <literal>close()</literal> ""marks the end of a session. The main implication of <literal>close()</""literal> is that the JDBC connection will be relinquished by the session. ""This Java code is portable and runs in both non-managed and JTA environments."msgstr """你不需要显式<literal>flush()</literal><literal>Session</literal> - 对""<literal>commit()</literal>的调用会自动触发session的同步(取决于session的""<xref linkend=\"objectstate-flushing\">FlushMode</xref>)。调用 <literal>close""()</literal> 标志session的结束。<literal>close()</literal>方法重要的暗示是,""<literal>session</literal>释放了JDBC连接。这段Java代码在非托管环境下和JTA环境""下都可以运行。"#. Tag: para#: transactions.xml:457#, no-c-formatmsgid """A much more flexible solution is Hibernate's built-in \"current session\" ""context management, as described earlier:"msgstr """更加灵活的方案是Hibernate内置的\"current session\"上下文管理,前文已经讲过:"#. Tag: programlisting#: transactions.xml:462#, no-c-formatmsgid """<![CDATA[// Non-managed environment idiom with getCurrentSession()\n""try {\n"" factory.getCurrentSession().beginTransaction();\n""\n"" // do some work\n"" ...\n""\n"" factory.getCurrentSession().getTransaction().commit();\n"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -