休眠多个用户,动态变化 [英] Hibernate multiple users, dynamically changing

查看:108
本文介绍了休眠多个用户,动态变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

技术上这里有两个问题,但是紧密结合:)我在一个新项目中使用了Hibernate。这是一个 POS 项目。
它使用Oracle数据库。

我们已经决定使用Hibernate,因为项目很大,并且因为它提供了(最流行的)ORM功能。现在Spring是不可能的 - 原因是:该项目是一个Swing客户端 - 服务器应用程序,它增加了不必要的复杂性。而且,Spring应该对硬件资源非常渴望。



有可能抛弃Hibernate并使用JDBC。为什么?项目要求是精确的数据库交互。这意味着,我们应该完全控制连接,会话和事务(并且,是的,和未优化的查询一样低)。

第一个问题是 - 第二个问题围绕着Hibernate展开。



>我们开发了一个简单的Hibernate试点项目。
另一个项目要求是 - 每个用户一个数据库用户/一个连接/每个用户/一个事务一个会话是灵活的(我们可以在需要时作为会话结束)

多个用户可以同时登录应用程序



我们完成了类似的任务。确切地说,我们在没有多用户要求的情况下实现了完整的功能。



现在,查看可用资源,如果我们要在数据库上有多个用户(在同一个模式上),我们将最终使用多个 SessionFactory ,为新的用户连接实现动态的 ConnectionProvider 。为什么?

用户散列密码在数据库中,因此我们需要动态地将用户添加到当前用户列表中。



第二个问题是 - 这可以做得更容易一些,Hibernate不支持这种配置似乎很奇怪。



谢谢。 / p>

解决方案

如果您在思考如何使用Hibernate或JDBC的天气,那么应该诚实地使用JDBC。如果您的域模型不太复杂,那么使用休眠并不会带来很多好处。另一方面,使用JDBC将极大地提高性能,因为您可以更好地控制查询,并且不会占用所有Hibernate开销,从而减少内存使用量。平衡这个我尽可能详细的模型的第一scetch。如果你能够从一开始就完成所有工作(没有可能在整个项目中疯狂改变的部分),并且如果所述模型看起来不涉及,JDBC将成为你的朋友。



关于您的用户和会话,我认为您可能会误会(但它可能只是我),但我认为您不需要多个SessionFactory来拥有多个会话。 SessionFactory是一个很重要的初始化对象,但是一旦你有了一个,你可以从它中获得多个轻量级的hibernate会话对象。



最后一句话,如果你真的坚持使用ORM解决方案(无论出于何种原因),如果可能的话,选择EclipseLink JPA2实现。 JPA2比hibernate具有更多的功能,而Eclipselink实现在休眠之后就没有多少bug了。


There are technically two questions here, but are tightly coupled :)

I'm using Hibernate in a new project. It's a POS project. It uses Oracle database.

We have decided to use Hibernate because the project is large, and because it provides (the most popular) ORM capabilities.

Spring is, for now, out of the question - the reason being: the project is a Swing client-server application, and it adds needless complexity. And, also, Spring is supposed to be very hungry on the hardware resources.

There is a possibility to throw away Hibernate, and to use JDBC. Why? The project requirement is precise database interaction. Meaning, we should have complete control over the connections, sessions and transactions(and, yes, going as low as unoptimized queries).

The first question is - what are your opinions on using the mentioned requrement?

The second question revolves around Hibernate.

We developed a simple Hibernate pilot project. Another project requirement is - one database user / one connection per user / one session per user / transactions are flexibile(we can end them when we want, as sessions). Multiple user can log in the application at the same time.

We achived something like that. To be precise, we achived the full described functionality without the multiple users requirement.

Now, looking at the available resources, I came to a conclusion that if we are to have multiple users on the database(on the same schema), we will end up using multiple SessionFactory, implementing a dynamic ConnectionProvider for new user connections. Why?

The users hashed passwords are in the database, so we need to dynamically add a user to the list of current users.

The second question is - can this be done a little easier, it seems weird that Hibernate doesn't support such configurations.

Thank you.

解决方案

If you're pondering about weather to use Hibernate or JDBC, honestlly go for JDBC. If your domain model is not too complex, you don't really get a lot of advantages from using hibernate. On the other hand using JDBC will greatly improve performance, as you have better control on your queries, and you get A LOT less memory usage from not habing all the Hibernate overhead. Balance this my making an as detailed as possible first scetch of your model. If you're able to schetch it all from the start (no parts that are possible to change wildly in throughout the project), and if said model doesn't look to involved, JDBC will be your friend.

About your users and sessions there, I think you might be mistaking (tho it could just be me), but I don't think you need multiple SessionFactories to have multiple sessions. SessionFactory is a heavy object to initialize, but once you have one you can get multiple hibernate session objects from it which are lightweight.

As a final remark, if you truly stick with an ORM solution (for whatever reason), if possible chose EclipseLink JPA2 implementation. JPA2 has more features over hibernate and the Eclipselink implementation is less buggy then hibernate.

这篇关于休眠多个用户,动态变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆