In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to bind JNDI with Hibernate". In daily operation, I believe many people have doubts about how to bind Hibernate to JNDI. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to bind JNDI in Hibernate". Next, please follow the editor to study!
The Hibernate binding JNDI name is implemented in the net.sf.hibernate.impl.SessionFactoryObjectFactory program. Let me analyze the process of Hibernate binding JNDI:
When we get SessionFactory, we usually write code like this:
Configuration conf = new Configuration (). AddClass (Cat.class); SessionFactory sf = conf.buildSessionFactory ()
First, new Configuration () creates a Configuration, reads the configuration file (hibernate.properties) in the constructor, and then saves it to a Properties object. This attribute is related to JNDI:
Hibernate.session_factory_name hibernate/session_factory
The buildSessionFactory () method is then called, which checks the configuration information and then invokes one of SessionFactoryImpl's constructors. Notice the following two lines of code in the constructor:
Name = properties.getProperty (Environment.SESSION_FACTORY_NAME); SessionFactoryObjectFactory.addInstance (uuid, name, this, properties)
The addInstance method of SessionFactoryObjectFactory is called and itself (an instance of SessionFactory) is passed as a parameter. * you can see the following code in addInstance method:
Context ctx = NamingHelper.getInitialContext (properties); NamingHelper.bind (ctx, name, instance)
Instance is an instance of SessionFactory. By reading the source code, you can clearly see that Hibernate binds the created SessionFactory instance to the name specified by the hibernate.session_factory_name attribute in the configuration file (hibernate.properties) through a series of class method calls when conf.buildSessionFactory (). Therefore, it can be seen that the Hibernate binding JNDI,Hibernate itself has the function of dynamic binding. But Hibernate needs to get a SessionFactory instance for binding, and this SessionFactory instance requires us to write code to create it in advance, and we must make sure that the process is done before all other programs that want to get a SessionFactory instance from JNDI.
So for any App Server, we don't have to worry about the binding process of JNDI names, we just need to make sure to create an instance of SessionFactory in advance, and Hibernate will do the rest. So how to ensure that the SessionFactory instance is pre-created? if it is Servlet, you can configure an initialized Servlet.
Configuration conf = new Configuration (). AddClass (Cat.class); SessionFactory sf = conf.buildSessionFactory ()
Just add this kind of code. If it is a complex J2EE application that includes EJB, you may need to rely on the functionality of App Server to ensure that SessionFactory instances are pre-created.
At this point, the study on "how to bind Hibernate to JNDI" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.