SOLR dataimport data import source code analysis (9)

Source: Internet
Author: User

The above SOLR dataimport source code mainly implements the Data Reading Function

Next we will look at the source code of data connection implementation:

Private connection getconnection () throws exception {
Long currtime = system. currenttimemillis ();
If (currtime-connlastused> conn_time_out ){
Synchronized (this ){
Connection tmpconn = factory. Call ();
Closeconnection ();
Connlastused = system. currenttimemillis ();
Return conn = tmpconn;
}

} Else {
Connlastused = currtime;
Return conn;
}

}

The synchronized keyword is used to enable
Conn access
Thread security. Factory is a callable <connection> type object generated in the initialization method.

The callable interface of Java is defined as follows:

Public interface callable <v> {
V call () throws exception;

}

Factory Initialization

Public void Init (context, properties initprops ){
// Other Code omitted

Factory = createconnectionfactory (context, initprops );
}

The createconnectionfactory (context, initprops) code is as follows:

Protected callable <connection> createconnectionfactory (final context,

Final properties initprops ){
// Final variableresolver resolver = context. getvariableresolver ();
Resolvevariables (context, initprops );
Final string jndiname = initprops. getproperty (jndi_name );
Final string url = initprops. getproperty (URL );
Final string driver = initprops. getproperty (driver );

If (url = NULL & jndiname = NULL)
Throw new dataimporthandlerexception (severe,
"Jdbc url or JNDI name has to be specified ");

If (driver! = NULL ){
Try {
Docbuilder. loadclass (driver, context. getsolrcore ());
} Catch (classnotfoundexception e ){
Wrapandthrow (severe, e, "cocould not load driver:" + driver );
}
} Else {
If (jndiname = NULL ){
Throw new dataimporthandlerexception (severe, "one of driver or jndiname must be specified in the data source ");
}
}

String S = initprops. getproperty ("maxrows ");
If (s! = NULL ){
Maxrows = integer. parseint (s );
}

Return factory = new callable <connection> (){
Public connection call () throws exception {
Log.info ("Creating a connection for entity"
+ Context. getentityattribute (dataimporter. Name) + "with URL :"
+ URL );
Long start = system. currenttimemillis ();
Connection c = NULL;
Try {
If (URL! = NULL ){
C = drivermanager. getconnection (URL, initprops );
} Else if (jndiname! = NULL ){
Initialcontext CTX = new initialcontext ();
Object jndival = CTX. Lookup (jndiname );
If (jndival instanceof javax. SQL. datasource ){
Javax. SQL. datasource = (javax. SQL. datasource) jndival;
String user = (string) initprops. Get ("user ");
String pass = (string) initprops. Get ("password ");
If (user = NULL | user. Trim (). Equals ("")){
C = datasource. getconnection ();
} Else {
C = datasource. getconnection (user, pass );
}
} Else {
Throw new dataimporthandlerexception (severe,
"The JNDI name: '" + jndiname + "' is not a valid javax. SQL. datasource ");
}
}
} Catch (sqlexception e ){
// Drivermanager does not allow you to use a driver which is not loaded through
// The Class Loader of the class which is trying to make the connection.
// This is a workaround for cases where the user puts the driver jar in
// SOLR. Home/lib or SOLR. Home/CORE/lib directories.
Driver d = (driver) docbuilder. loadclass (driver, context. getsolrcore (). newinstance ();
C = D. Connect (URL, initprops );
}
If (C! = NULL ){
If (Boolean. parseboolean (initprops. getproperty ("readonly "))){
C. setreadonly (true );
// Add other sane defaults
C. setautocommit (true );
C. settransactionisolation (connection. transaction_read_uncommitted );
C. setholdability (resultset. close_cursors_at_commit );
}
If (! Boolean. parseboolean (initprops. getproperty ("autocommit "))){
C. setautocommit (false );
}
String transactionisolation = initprops. getproperty ("transactionisolation ");
If ("transaction_read_uncommitted". Equals (transactionisolation )){
C. settransactionisolation (connection. transaction_read_uncommitted );
} Else if ("transaction_read_committed". Equals (transactionisolation )){
C. settransactionisolation (connection. transaction_read_committed );
} Else if ("transaction_repeatable_read". Equals (transactionisolation )){
C. settransactionisolation (connection. transaction_repeatable_read );
} Else if ("transaction_serializable". Equals (transactionisolation )){
C. settransactionisolation (connection. transaction_serializable );
} Else if ("transaction_none". Equals (transactionisolation )){
C. settransactionisolation (connection. transaction_none );
}
String holdability = initprops. getproperty ("holdability ");
If ("close_cursors_at_commit". Equals (holdability )){
C. setholdability (resultset. close_cursors_at_commit );
} Else if ("hold_cursors_over_commit". Equals (holdability )){
C. setholdability (resultset. hold_cursors_over_commit );
}
}
Log.info ("time taken for getconnection ():"
+ (System. currenttimemillis ()-Start ));
Return C;
}
};
}

The connection object is generated here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.