×

Loading...
Ad by
Ad by

这个误导太严重,不得不回了。Hibernate提供三种提取currentSession的方法,一种是绑定在ThreadLocal上,一种是绑定在JTA transaction上,一种是自定义。因为80%以上的enterprise application都使用application server, 所以最常用的应用都是绑定在JAT transaction上。

本文发表在 rolia.net 枫下论坛1,你如果使用application server,另外重复绑定hibernate session到ThreadLocal,那就表明你不明白Hibernate的设定。因为在Hibernate 3.0里面,如果你的session transaction定义为JTA(一般使用application server都会用它的JTA manager),那么Hibernate自动绑定hibernate session到JTA transaction,不需要做任何设定。

2,绑定在ThreadLocal和JTA Transaction,不能实现conversational transaction。应该不用解释了吧。所以用这种方法来实现conversational transaction, 那是不行的。

3,只能自定义。hibernate session可以绑定在http session上,可以绑定在stateful bean上,anything which makes sense。至于httpsession close时候如何close hibernate的session,
a. 你如果确实关心这个问题的话,你如果知道servlet 2.5标准,就不会问出这个傻问题。servlet 2.5标准里面定义了无数的listener,自己去查。
b. 你如果知道flush, commit, close的区别,都不会去关心这个问题。

4,看来国人计算机不行的主要原因是看不懂英文专业技术书籍,一遇到事情就开始瞎猜,七嘴八舌地好像记得应该以前做过之类,说不出根本原因。当然很少有人牛逼到一遇到问题就查源程序的地步,所以说熟悉专业技术书籍,特别是reference book,很重要,绝大部分时候查资料比查源程序快得多。当然很多人会解释说只会看书不会动手,没用,所以为了避免不会动手,就坚决动手不看书。这个逻辑很convenient,但是会让你变傻。更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 书上说如果完全不用lazy-load得话,
    会因为对象之间的互相引用而在查询时候不得不查询大量的不必要数据,若是引用关系较多,数据对象之间的引用关系就像是一张蜘蛛网,说不定查单一对象就得遍历所有的表。

    请问大伙实际用HIBERNATE 时,真的都把LAZY-LOAD 设成FALSE 吗? 谢谢指教。
    • 悟空JJ (part time 买房gent?)在楼下的帖子说的有理。因为对于web应用程序,一个request处理完了,session就结束。如果用lazy loading, 你会得到invalid session exception. (这也是面试常见问题)。
      你可能会问,可否将session 保持open. 理论上行,实际上不,因为这将影响到所谓的 scalability.
      • james05大侠所说的 SCALABILITY是指不同的JVM 吗?
        如果在同一个JVM 里 OPEN SESSION IN VIEW 也不好吗?
      • "一个request处理完了,session就结束。" --- You sure...?
        • Yes, as a best practice, after a HTTP request is handled, it's best to close the Hibernate session.
          Please do not confuse the Hibernate session with HTTP session. 如果你不懂,我可以与你分享一些网上资源。
          • long running session (conversational transaction)你不知道?hibernate session在request结束之后 为什么一定要close? 你要知道commit, flush, 和close的区别。
            • You are the only one who is correct in this discussion.
          • The lifecycle of a session is bounded by the beginning and end of a logical transaction. Entity instances may be stored in HTTP session as session variables and may be shared by upcoming requests.
            you joke here.
            • 如果在多个SERVLET REQUEST中重复使用一个HIBERNATE SESSION(把HIBERNATE SESSION 放到HTTP SESSION里面),就必须处理在HTTPSESSION超时后强制关闭HIBERNATE SESSION这种特殊情况
              • I read james05's post again, yes, it is best practice to close any transaction object whenever http request is completed, or, as a final defense, check and close it when session is destroyed.
              • long running session (conversational transaction)你不知道?hibernate session在request结束之后 为什么一定要close? 你要知道commit, flush, 和close的区别。 -crashingwaves(CherryHill); 19:31 (#5640803@0) Reply
                • If you have low number of concurrent users, what you said is okay. But imagine the system needs to support hundreds, even thousands, of concurrent users. What you said is not scalable. See also the notes below posted by ejbEqHouse。
                  • 这和concurrency有什么关系,我倒是非常非常好奇了。大家是不是找到了servlet 2.5标准的一个大bug,或者是找到了JTA的一个大bug,震撼,非常震撼。麻烦大家解释一下。
            • 老兄, 你的程序真是这么写的?
              • 我没这么写,我是顺着牛哥的思路怎么想
              • 写不好,瞎写,向大虾们学习.:-)
              • 牛哥说的没错,你难道不是这样设计的?
                • To close a session is to release the JDBC connection. If you put a hibernate session into a http session, so you do not close it for a long time. Then why you need connection pool?
                  I never put a hibernate session into a http session as a http session variable. If we do so, why we need n-tier? What is session facade and what is DAL/DAO?
                  • 不关闭HIBERNATE SESSION
                    • SORRY, AGAIN. 不关闭HIBERNATE SESSION , 也可以关闭JDBC CONNECTION, 用SESSION.DISCONNECT() , 需要的时候再连上SESSION.RECONNECT()
                      • Firstly, reconnect() is deprecated, you should use reconnect(Connection connection). How do you get a connection when you try to reconnect? I do not use this way, so I have no idea about it.
                        I alway close a session when I finish a query or a transaction. In fact, I will open a session and close a session multiple times to handle a http request.
                        Secondly, all my database operations are handle in the data access layer. I donot think that it is good to put hibernate session into a http session. If to do so, we do not need n-tier.
                        • "I alway close a session when I finish a query or a transaction. In fact, I will open a session and close a session multiple times to handle a http request." -- 你太厉害啦!
                          • Ok. Show you a example. In a request, I need to do more than 20 business logical checking which involve multiple tables and each of those validation methods are in different class so that they are reusable.
                            I do not think that we should pass a hibernate session between those validations. For each validation, I open a session and close it after I finish my query. It is so normal. Even you load a page for a request, you can not load all data in one class and one method except your application/database is not complex.
                            • 那很显然,你说的scenario就应该不是我们讨论的case了,对不对?
                              • what I said is that we should not put a hibernate session into a http session.
                                • Right, then we need to put it somewhere else, correct? No where else can we put it?
                                  • Modern frameworks support this feature. Both Spring Web Flow and SEAM support conversation scope, which is better than http session.
                                    • Let me explain what conversation scope is, it is smaller than http session scope. It is more likely to group some related request together.
                                      • I read your posts again and find I might have misread your opinion. You actually only have problem with putting hibernate session into http session. If you have a framework like SEAM or SWF 2.0, where conversation is available, you
                                        are right. But, if you don't have such framework support, I wonder how you answer CherryHill's question of where you put the hibernate session. If you really drill down, you can see SEAM and SWF 2.0 are putting hibernate session into http session to implement conversation scope.
                            • I assume that what you mentioned "business logical checking" is a series of DB read operation, no commit is needed, then why don't you complete them one by one in ONE session?
                              • Sorry, that is too much detail information. I need to work on my ETL applcation. I have a demo tomorrow morning.
                                In one word, we need to access multiple tables and those methods are need to be reusable.
                                • OK, understand. Thanks! In many scenarios, mid tier wrappers are heavier and heavier and are over-kill. absolutely a wrong way to treat db like a procedural process. The backbone should be high efficient SQL query, not reusable object.
                        • reconnect() is deprecated because hibernate reconnects automatically for you and you don't have to call reconnect() by yourself.
                          • Could you give me a link about it? Anyway here is from hibernate API which is different what you said:
                            Deprecated. Manual reconnection is only needed in the case of application-supplied connections, in which case the reconnect(java.sql.Connection) for should be used.
                            • How is it different? "Manual reconnection is only needed in case of..." means you don't need to reconnect manually unless ... You don't need because it is done by hibernate.
                  • 我这样设计: conn pool is global accessible, session picks a conn and release it right away when transc is done. you may save dat in cache in mid tier, so no conn creation is needed for better perf.
                    my understanding, most obejct-data -boundles are session-specific. means they may be different in content, format based on different user/role.
                  • 震撼了,我非常地震撼了。surrounded by such stupidity is a very very strong feeling...不close Hibernate session就不能释放connection。苍天啊,拯救这帮迷途的羔羊吧,请赐予他们英文阅读能力,让他们能看懂书吧。
                    • So you put the hibernate session into a http session as a http session veriable? I do not know how you think about DAL/DAO.
                      And reconnect() is deprecated, you should use reconnect(Connection connection). How do you get a connection when you try to reconnect? Maybe you could answer those two questions to me. I really would like to know.
                      • It's a joy that 手里拿着Christen Bauer的书,看着你们在这里信口开河。It is really a joy。If you can't read the source code, read the book。Christen & Gavin don't lie, since they designed Hibernate。
                        • My application got a province gold award. And I also finished a SEAM project. I am working on database synchronization and ETL. I do not know what you are talking about.
                          What I said is based on my experience. LOL, put your hibernate session into your http session.
                          • I seriously doubt you worked in a SEAM project. Perhaps, you were not a lead or senior member in the project. Have you ever used conversation scope, which is one of the most selling point in SEAM?
                            • I finished the SEAM project last year. It is one of the best in our company's applications. I build the application from beginning and design the key part of it.
                              I donot work on it for a while. Give you a detail information: the Crystal Reports is not integrated with SEAM well last year. Dose it improve now?
                              • Good for you. You may want to refresh your memory and take a look into the chapters about SEAM managed transaction in any SEAM book or its reference. SEAM was the first framework supporting extended session in conversation scope, which
                                is common now.
                                • Let me explain what conversation scope is, it is smaller than http session scope. It is more likely to group some related request together. -ejbeqhouse(ejbEqHouse); 22:42 (#5641383@0)
                                • I do not copy the explanation from anywhere. It is the concept in my mind.
                                • Did you work on SEAM? Could answer my question about how integrate the report with it? If you did not work on it, you should not know right away. However it impressed me so much, even more than any other concept.
                                  • I did not integrate SEAM with Chrystal Report.
                        • 樱桃,听我劝一句。Christen Bauer的书 -- Yes I understand that he is one of the main contributors to Hibernate -- can only be treated as a text book. There are many problems it does not address for an enterprise application.
                          So are Java / J2EE books. That's why we have best practices.
                          • You know what you are talking about, right?
                            • 嗯, I know. BTW, 这里是技术讨论。俺怎么感觉你跟其他的朋友有点不一样呢?大伙儿的经验,知识可能不一样,谁对谁错并不是最重要的,但只要大家经过讨论,互相学习,共同有所提高,这才是Sailor的宗旨之一。
                              真正的高手,就应该拿得起,放得下。
                              • +1 to the attitude. Open and healthy discussions help everyone! And, that is the only reason I am here.
                              • +1. hibernate session 放在 http session 里不是不可以, 但90%以上的用法不会这么做。最常见的就是对于每个request把hibernate session绑在thread local上用完之后close。
                                某些搞技术的国人毛病就是不够坦诚,有什么说什么就是了,觉得自己看了几本书,就非得云山雾罩高来高去的,有什嘛意思。

                                别回复哥,哥也不会再回复,飘过。。。
                                • 感谢理解。If you use a J2EE App Server, there is another way - let the app server handle all these routine stuffs for you. See the so-called "正解" of mine in the lower part of this msg thread.
                                • 这个误导太严重,不得不回了。Hibernate提供三种提取currentSession的方法,一种是绑定在ThreadLocal上,一种是绑定在JTA transaction上,一种是自定义。因为80%以上的enterprise application都使用application server, 所以最常用的应用都是绑定在JAT transaction上。
                                  本文发表在 rolia.net 枫下论坛1,你如果使用application server,另外重复绑定hibernate session到ThreadLocal,那就表明你不明白Hibernate的设定。因为在Hibernate 3.0里面,如果你的session transaction定义为JTA(一般使用application server都会用它的JTA manager),那么Hibernate自动绑定hibernate session到JTA transaction,不需要做任何设定。

                                  2,绑定在ThreadLocal和JTA Transaction,不能实现conversational transaction。应该不用解释了吧。所以用这种方法来实现conversational transaction, 那是不行的。

                                  3,只能自定义。hibernate session可以绑定在http session上,可以绑定在stateful bean上,anything which makes sense。至于httpsession close时候如何close hibernate的session,
                                  a. 你如果确实关心这个问题的话,你如果知道servlet 2.5标准,就不会问出这个傻问题。servlet 2.5标准里面定义了无数的listener,自己去查。
                                  b. 你如果知道flush, commit, close的区别,都不会去关心这个问题。

                                  4,看来国人计算机不行的主要原因是看不懂英文专业技术书籍,一遇到事情就开始瞎猜,七嘴八舌地好像记得应该以前做过之类,说不出根本原因。当然很少有人牛逼到一遇到问题就查源程序的地步,所以说熟悉专业技术书籍,特别是reference book,很重要,绝大部分时候查资料比查源程序快得多。当然很多人会解释说只会看书不会动手,没用,所以为了避免不会动手,就坚决动手不看书。这个逻辑很convenient,但是会让你变傻。更多精彩文章及讨论,请光临枫下论坛 rolia.net
                                  • 握手!
            • Out of curiosity, can the logical transaction in your framework cross multiple HTTP requests?
              Your statement here is only correct when your answer is yes. But, from your other posts in this thread, I guess your answer is no.
          • 我觉得james大侠的意思是关掉HIBERNATE SESSION后,反正可以创建一个新的SESSION去绑定DETACHED 的对象(如果需要读后续内容)
            • 你自己先把关掉session是什么意思搞清楚再说。
              • 我觉得还是 OPEN SESSION IN VIEW 应该比较好,各位大侠的立场在不同场景都有合理的地方,呵呵 和和稀泥。。。
      • session在.net里有in-proc, sqlsession, sqlserver三种, 至少用sqlserver的session不影响scalability, 不知道java的也应该有类似机制吧.
    • 书上还说,如果不用lazy loading, 你应该设定fetching strategy。