Hibernate's JNDI Binding Analysis

xiaoxiao2021-03-06  42

Hibernate's JNDI Name Bind is implemented in the net.sf.hibernate.impl.SessionFactoryObjectFactory program, I will analyze the process of hibernate's binding JNDI: We get sessionFactory usually write code: configuration conf = new configuration () .addclass (cat.class);

SessionFactory sf = conf.buildsessionFactory ();

The first is new configuration () creates a configuration, performs the read work of the configuration file (Hibernate.properties), then saved to a Properties object, and JNDI is related to this property: hibernate.Session_Factory_Name Hibernate / Session_Factory

Next, the buildSessionFactory () method is called. This method checks the configuration information and then calls a constructor of the sessionFactoryImpl. Note the following two lines of code inside the constructor: name = property.getProperty (Environment.Session_Factory_Name);

SessionFactoryObjectFactory.AddInstance (UUID, Name, this, Properties);

The AddInstance method of the sessionFactoryObjectFactory is called and the self (SESSIONFACTORY instance) is passed as a parameter. Finally, you can see the following code in the AddInstance method: context ctx = naminghelper.getinitialContext (property);

NamingHelper.bind (CTX, Name, Instance);

Instance is the instance of SessionFactory. By reading source code, you can clearly see Hibernate is called by a series of class method instances when conf.buildsessionFactory (), binds the created sessionFactory instance to the configuration file (Hibernate.properties) The .Session_Factory_Name property is on the name specified, so it can be seen that Hibernate itself is a dynamic binding function with JNDI. But Hibernate needs to get a sessionFactory instance for binding, and this sessionFactory instance requires us to write code for pre-creation, and must ensure that the process is to be completed before all others have to get a SESSIONFACTORY instance from JNDI. Therefore, we don't have to deal with the binding process of the JNDI name, just guarantee that it is enough to prepare a sessionFactory instance, and the remaining work hibernate will do. So how do you make sure you create a sessionFactory instance, if you are servlet, you can configure a initialized servlet, just put configuration confise (new configuration (). Addclass (Cat.Class);

SessionFactory sf = conf.buildsessionFactory ();

Such code can be added. If it is a complex J2EE app containing EJB, you may need to rely on the application of App Server to ensure the prerequisite for the SESSIONFACTORY instance.

转载请注明原文地址:https://www.9cbs.com/read-66554.html

New Post(0)