Transfer from: CNJBB Forum JDBC column of Axman Heroes Write -Datasource Ultra Advanced Application Link: http://www.cnjbb.org/thread.jsp? BoardId = 23 & threadid = 34286 In the previous introduction, we can see that DataSource can provide the highest performance to the database's concurrency, but the reference to DataSource, there are also many knowledge to make it clear that how the connection is the most relevant technology, and the DataSource's call is played. Very big decisive role. Generally, the reference to DataSource is performed by the following procedure: context ct = new initialContext (); DataSource DS = (Datasource) ct.lookup (SourceURL); as simple in two rows, but its call criteria can produce performance Huge difference. Because a CONNECT package class (bean is a reference to DataSource), there will be a variety of ways. If it is a normal package class, we can call in the constructor, and as a JavaBean can be called in the init method, but whether it is called, when we call this class or bean, we must find DataSource. . In fact, DataSource's search process is also a fairly resource process, so we should declare DataSource as static resources, because even if you are looking for each call in the package or bean, in fact you get DataSource or that. The only resource.
So why not declare it as static? This only goes to find it only when needed, and more, you can directly reference the DataSource that has been found: public class connectFactory {private static datasource dscache = null; AMETHODFORGETCONNECTIO () {ix (DSCache == Null) {Synchronized (this) {if (DSCache == null) {context ct = new initialContext (); dscache = (datasource) ct.lookup (source);}}} connection conn = dscache.getConnection ();}} We see In general, only when the ConnectionFactory will look for DataSource when the ConnectionFactory is called, and when there is no accident, when I can't think of any situation, it will not be NULL Datasource sudden null. Then you need to find it directly. And if you don't declare DataSource as static, then a JNDI lookup every time you call ConnectionFactory: public class connectFactory {Private Datasource DSCache = NULL; AMETHODFORGETCONNECTIO () {ix (DSCache == NULL) {Context CT = New InitialContext (); DSCache = (Datasource) ct.lookup (Source);} Connection Conn = DSCache.getConnection ();}} Because DSCache is not static resources, each call, IF ( DSCache == NULL) is set up. Some people say that if the connection is also static, it does not save Is there a resource? Note that Connection is not a workshop. If multiple users are accessible to the same connection, then anyone can't turn off the connection, and finally don't close the connection, because the last used users don't know if someone is To use. On the other hand, a Connection handling ability is very low, it is impossible to meet many users at the same time to taking data inventory. Although DataSource is a reference to the same resource, it is a factory, different users visit the same Datasource. Get different connection resources. In order to find specific DataSource, we have to configure JNDI's reference. In general, if a package or bean does not likely to write a JNDI reference string to die, it can be placed Configuration files and read: Properties Properties = New Properties (); Properties.Load (New FileInputStream ("Profile")); DSCache = (Datasource) ct.lookup (Properties.getProperty ("key"); this is When we want to modify DataSource, you can modify the configuration file.
Not to recompile ConnectionFactory, but if you are configured with multiple DataSource at the same time, we can't specify which data source to check, it is best to overload a method, whether it is a constructor or other the methods should override a parameter lookup method: public class ConnectionFactory {private static DataSource dsCache = null; aMethodForGetConnectio () {if (dsCache == null) {synchronized (this) {if (dsCache == null) {Properties properties = New Properties (); Properties.Load (New FileInputStream ("Profile")); Context CT = New InitialContext (); DSCache = (Datasource) ct.lookup ("Properties.getProperty (" Key ");}}} Connection conn = dsCache.getConnection ();} aMethodForGetConnectio (String source) {if (dsCache == null) {synchronized (this) {if (dsCache == null) {Context ct = new InitialContext (); dsCache = (DataSource) Ct.lookup (Source);}}} connection conn = dscache.ge Tconnection ();}} In addition to the default lookup string, we can still do further optimization, but even if each read attribute, performance does not have much impact: public class connectFactory {private static datasource dscache = null; private static Properties properties = new Properties (); aMethodForGetConnectio () {if (dsCache == null) {synchronized (this) {if (dsCache == null) {if (! properties.containsKey ( "key")) properties.load ( New fileInputStream ("Profile")); // Only to read the configuration file CONTEXT CT = new initialContext (); dscache =