Transfer from the JDBC column of the CNJBB Forum, the advanced application of the Axman heroes written -JDBC: http://www.cnjbb.org/thread.jsp? BoardId = 23 & threadid = 24492 We said that there is JDBC advanced application, not It is said that it has a high technical content (maybe there is no "technical content" on the Java platform, because Java is for everyone, not for some people). Say it is an advanced application, because it is The expansion of JDBC basic applications, that is, you can optimize your application performance, or convenient for application implementation. So it is an advanced application without a high-level technology. In JDBC, java.sql package is based, too The core function, Javax.sql package is advanced, extended features. So in order to communicate, we divide them into Core API and Optional API. But there is still some of the functions in the Core API, I will summarize it. Advanced applications. The call of the memory process, the process of multi-result set, I have to take these things out, I can't find any detailed documents and routines online, I can say that there is 95%. The above developers don't know how truly handle these work. So I will write this paragraph after returning to Shanghai. Now let's take a look at the benefits of the Optional API: We have already understood, execute a SQL statement To pass through the following steps: 1.Connction 2.Statement 3.Statement.executexxxxx (); 4. Optional to process for results set 5. The necessary Connction is closed. (Remind again if you want to be intermediate level Programmer, please write the closed statement in the finally block, I will introduce a method to verify that your program has a link to the most important job, generating the CONNCTION object is the most important job. It is also the most resource consumable, because the connection object is to drive the underlying socket, call the physical link and the database to communicate, so generate, close, regenerate into such a connection object is equivalent to our in the 1980s (1980). Don't know it?) Drinking can drink. You buy a bottle of drinks is a two-corner. You know the CONNECTION value of a piece of zero. And the things you drink are only worth two points, this is We have a real data of a beverage plant. Before the Javax.sql package, we can only buy such a drink, unless you don't drink. There are also some people who don't take themselves to produce drinks (Poolman), but consumers are very Fast discovery, it just puts the original single-selling Vellant now to us, because it is also produced by the original packaging raw material, and the poolman of Poolman is from getConnection from DriverManager. Really Efficiency If it is not your psychological effect, it may be lower than a single link !!! and some "masterpieces" of the rivers and lakes, they can't jump out of this frame. I have been drunk in that time, I have been drunk in studying these "connecting pools".
Because everyone can read someone else's original code, plus other people's advantages to write a better come, but everyone can see, I wrote UPLOAD, Download, Htmlutil, Encoder, A series of tools such as Decoder. However, I didn't write a successful link .... Let's go deep into a step, why DriverManager generated and its connecting pool cannot really improve performance. DRIVERMANAGER object, vast majority JDBC is encapsulated a physical link, which also grabs a Socket that communicates with the database. When you use drivermanager.getConnection (), there is a Socket with a database to let you take up. And this method is synchronous Everyone knows that such physical links are restricted to any system, such as a web server generally has a maximum of 150 to 250, the database server is also the case, you must not only consider your program, you can't use the light, To consider different Runtime or other applications are also using these physical links with you. If there is Java Web Server on a server, there is also a C's application to access the database, you can't ask people to ask people C Programmer His program must wait for your Java call idle to access the database. So the physical link is extremely valuable. The connection pool based on DriverManager.getConnection () is only a Physical link to a pool, then Number, etc., it is omitted to generate such a connection time. Note that the connection you get before you didn't release it, because the junction of the handle is in the user's hand, and this connection is called The program caller is initialized, each call must have initialization work, and whether the caller is running it with an optimized approach, complete the level of programming each person. On the other hand, if this connection is used Web container management, that is simple to use, because it is forced to use static variables to keep connecting, the container cannot be accessed at all, and it cannot be called in different runtimes. And Javax.sql implementation uses in user linkage and physics Connect the middle plus a buffered intermediate layer, although only 30 physical links, but the user does not access it, DataSource is returned to the user is a Java abstract object, the client program puts the connection request back the buffer in the DataSource unified scheduling Physical linkage is handled, so that the maximum procedure can use the valuable physical link. Maybe the 30 physical links are still not loaded. You still need to modify the actual connection, but we know that this connection is not DRI now. Vermanager.getConnection () can be better than. That is to say, DataSource can give us more (much more) call opportunities, in fact, normal case in Datasource, in fact, normal in DRIVERMANAGER The load of the physics is only a few percent, that is, because your call grabbed its handle and can't make it work very well. In addition, the programmer can only return it without closing it, because the traditional connecting pool Once the connection object is turned off by the user, it is necessary to regenerate the physical new link, so the user can only release, the connection to the non-connection pool and the connecting pool, and use different code programming, simple is a pain. Once there is no Note that this error is not released after processing the data, this error is the fault ???????????? I said that java.sql achieves the vast majority of physical links, and exceptions But not those previous connecting pools, but OSE is Oracle's servlet environment, which is to implement the servlet server on the address space of the database, and servlet services go to call the database without passing the traditional link, because the data is "open"
This is like accessing other computer files through the network and accessing the local file. Although it also provides a standard JDBC interface to let you call, it is not in JDBC package at the bottom of it. DataSource's additional advantage is that it fully implements the database And the application is separated, how to configure the server to generate DataSource's reference and program development, you are in program development, just use JDNI to find DataSource's logical name. And in DRIVERMANAGER.GETCONNECTION (), you have to use the database driver name , Access address, user, password written in your application, even if you can read these properties strings from the configuration file, you have to modify the path of different server profiles. And DataSource provides standard configuration. Say Say, how to connect the database with Datasource? Very simple: DataSource DS = (DataSource) new initialContext (). Lookup ("jdbc / mydb"); connection conn = ds.getConnection (); Of course I am to explain it simple intentionally Ignore it. In practical application. You should capture it an exception. If you still don't understand what is jdni, I advise you to find some information in this area, this can be the basic agreement in the network programming. Ah. About DataSource DS = (Datasource) New InitialContext (). Lookup ("JDBC / MyDB"); A few questions, you need to configure your server (Tomcat, Resin, WebLogic) all of these servers An example, if you don't understand it, you will change the example in a few words. You will slowly understand what you represent for a while. Then you call new initialContext () in the container environment. Lookup (" JDBC / MYDB ") The container will automatically find that DataSource object to call you, this process is transparent to the user. The smart friend has already asked, because DataSource's attributes are already configured in the container , Then I am not in the container environment, such as a separate application, how can I get DataSource? In fact, if you can know new initialContext (), the container calls what default configuration, you can handle these configuration parameters Add in and do not rely on the container environment. Good in InitialContext Environment (), after generating this object, you can get a look, record these parameters, and put it in the environment without these parameters. Talk about how many words, talk about learning methods, I hosted several people in China Forum (not much, two or three), never asked others, Java technology is not invented, it is impossible to understand what I understand, one is asked if there is a "master" style (haha, actually The real master is still asking others. Only I will not ask someone else this kind of fake master). On the other hand, I don't have to ask someone else, such as the application, connecting different manufacturers, DataSource to put what is going in? It is to go to their website to see information. Although my English level is only 10%, I may be more than you in English. II is to see if there is any shared API to get, so I can get GetEnvironment (), But if there is any way? This is going to see your learning attitude, some people will call "master", what else is "urgent, online waiting"
What. And I will compile new initialcontext () to see what it calls (Confucius said, in order to learn, it is permissible, even great, bright, correct thoughts, is Some moral, from the low-level fun, it is good for the people's behavior !!! ---- Confucius quotation picked 123 pages 43, 1989 Edition) If an object is required to have parameters, It has another construction method without parameters. You think it is definitely calling the default parameters in a construction method without parameters, you have to print it out. How many people do this? JDBC Optional Other extensions of the API: javax.sql is not only in performance, but also supports distributed transactions, in the traditional linking process, we can setAutocommit () in a link, then through Rollback () Or commit () returns and submits the transaction, which is called a local transaction during a linking process, but if you want to operate multiple databases in a transaction, or more servlet participation, then you must Using distributed transactions. About JDBC transactions I will put it here, a feature that is worth celebrating is that the transaction save point has been implemented in JDBC3.0. In the past, if we put transaction atoms A, B, C A transaction, if A, b is executed, C failed, we can only return A, B, but now we can save A, B as a point, then this point is rolling or submitted, This is like saving a copy at different times when writing an article in a different time, and there will be no words, or it is the state of the current editor. Now let's optimize the bean we implement in the basic knowledge. Today is at home. I can't in the forum, what is the name of the last written part of the last time I forgot, now we call it PooledDB. At that time, we have divided the bean into three parts, and the generation link is independent, while the business method department Don't move it at all, this is the benefits of inheritance:) package com.inmsg.beans; import javax.naming. *; Import javax.sql. *; Public class pooleddb {connection con = null; private string source = "" "; Public pooleddb () throws exception {// default constructor, if constructed, do not add parameters, link JDBC / Office SOUR CE = "java: comp / env / jdbc / office"; context ct = new initialContext (); DataSource DS = (DataSource) ct.lookup (source); con = ds.getConnection ();} // then add overload Method, used to link other data source public pooleddb (string source) throws exception {this.source = source; context ct = new initialContext (); DataSource DS = (DataSource) ct.lookup (source); con = ds.getConnection ();} / (Note Be sure to give Source to member variables this.Source, because there is another MakeConnection () // auxiliary method, if you do not assign Source to this.Source, makeconnection () calls the default Source string private void makeconnection () throws exception {context ct = new initialContext ();