Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: mill/db/DBconnect.java


1   package mill.db;
2   
3   import java.sql.Statement;
4   import java.sql.ResultSet;
5   import java.sql.Connection;
6   import java.sql.SQLException;
7   
8   import mill.tools.MainTools;
9   import mill.tools.ExceptionTools;
10  import mill.startup.InitJSPparam;
11  
12  public abstract class DBconnect
13  {
14    protected static DBconnect _db_ = null;
15    protected static Boolean   initFlag = new Boolean(false);
16  
17      private   static ConnectionData mainConnectionData_ = null;
18    private   static String  DBconnectClass_ = null;
19  
20      public  boolean   isDBOk = false;
21  
22    public  Connection  conn = null;
23  
24    protected String   login_ = null;
25    protected String   pass_ = null;
26    protected boolean   isCommit_;
27    protected boolean  isDriverLoaded = false;
28  
29    // non dynamic connect used for 'select' operation
30    // dynamic used for 'update,delete,insert' operation
31    protected boolean  isDynamicConnect = false;
32  
33    public abstract boolean getIsClosed() 
34      throws SQLException;
35    public abstract String getClobField(ResultSet rs, String nameFeld)
36      throws SQLException;
37    public abstract String getClobField(ResultSet rs, String nameFeld, int maxLength)
38      throws SQLException;
39    public abstract boolean testExceptionTableNotFound(Exception e);
40    public abstract boolean testExceptionIndexUniqueKey(Exception e, String index);
41    public abstract long getSequenceNextValue( String s )
42      throws SQLException;
43    public abstract long getFirstValue( String t, String f, String w, String o)
44      throws SQLException;
45    public abstract String getFirstValueString( String t, String f, String w, String o)
46      throws SQLException;
47    public abstract int getMaxLengthStringField();
48  
49  
50  
51      public static void setConnectionData(ConnectionData cd)
52      {
53    mainConnectionData_ = cd;
54      }
55  
56      public static ConnectionData getConnectionData()
57    throws Exception
58      {
59        if (mainConnectionData_ == null)
60    {
61        mainConnectionData_ = InitJSPparam.getMainConnectionData();
62        if (mainConnectionData_ == null)
63        {
64      String except = "Instance of 'mainConnectionData' not initialized. Check millennium.properties file.";
65      System.out.println( except );
66      throw new Exception( except );
67        }
68    }
69  
70    return mainConnectionData_;
71      }
72  
73      public static void setDBconnectClassName( String s ) {
74    DBconnectClass_ = s;
75      }
76  
77      public static String getDBconnectClassName()
78      {
79    if (DBconnectClass_ == null)
80        DBconnectClass_ = InitJSPparam.getDBconnectClassName();
81  
82    return DBconnectClass_;
83      }
84  
85  
86      public boolean isDynamic()
87      {
88    return isDynamicConnect;
89      }
90  
91      public synchronized static void terminateConnection()
92      {
93    if (_db_ != null && _db_.conn!=null)
94    {
95      try {
96         _db_.conn.close();
97         _db_.conn = null;
98      } catch (SQLException e) {}
99    }
100   mainConnectionData_ = null;
101   _db_ = null;
102     }  
103 
104     protected static DBconnect openDynamicConnect()
105   throws Exception
106     {
107   DBconnect db_ = null;
108 
109   try{
110     System.out.println("Call for create dynamic object " + getDBconnectClassName() );
111     db_ = (DBconnect)MainTools.createCustomObject( getDBconnectClassName(), false );
112     db_.conn.setAutoCommit( false );
113     db_.isDBOk = true;
114     db_.isDynamicConnect = true;
115     System.out.println("Success create dynamic object " + getDBconnectClassName() );
116   }
117   catch(Exception e)
118   {
119     if (db_!=null && db_.conn != null)
120     {
121         try{
122       db_.conn.close();
123       db_.conn = null;
124         }catch(Exception e02){}
125     }
126     db_ = null;
127 
128     System.out.println("\nError create instance for class "+getDBconnectClassName() );
129     System.out.println( 
130       ExceptionTools.getStackTrace(e, 15)
131     );
132     throw new Exception( e.toString() );
133   }
134   return db_;
135     }
136 
137     protected static DBconnect openConnect()
138   throws Exception
139     {
140      synchronized (initFlag)
141      {
142 
143      if (!initFlag.booleanValue() ) 
144      {
145 
146   initFlag = new Boolean(true);
147   try 
148   {
149       try
150       {
151         if ((_db_ != null) && (!_db_.getIsClosed()) )
152         {
153         return _db_;
154         }
155       }
156       catch(SQLException e1)
157       {
158     if (_db_!=null && _db_.conn != null)
159     {
160         try{
161       _db_.conn.close();
162       _db_.conn = null;
163         }catch(Exception e01){}
164     }
165     _db_ = null;
166       }
167 
168     String str = "";
169     for (int i=0; i<3; i++)
170     {
171        try{
172       System.out.println("Call for create static object " + getDBconnectClassName() );
173       _db_ = (DBconnect)MainTools.createCustomObject( getDBconnectClassName(), true );
174       _db_.isDBOk = true;
175       _db_.isDynamicConnect = false;
176       System.out.println("Success create static object " + getDBconnectClassName() );
177       break;
178        }
179        catch(Exception e)
180        {
181       System.out.println("\nError create instance for class "+getDBconnectClassName() );
182       System.out.println( e.toString() );
183       if (_db_!=null && _db_.conn != null)
184       {
185           try{
186         _db_.conn.close();
187         _db_.conn = null;
188           }catch(Exception e02){}
189       }
190       _db_ = null;
191       System.out.println( 
192         ExceptionTools.getStackTrace(e, 15)
193       );
194        }
195     }
196       } // try
197       finally { 
198     initFlag = new Boolean(false);
199       }
200           } // if (!initFlag)
201        } // synchronized (initFlag)
202   return _db_;
203     }
204 
205     public static DBconnect getInstance()
206   throws Exception
207     {
208   return getInstance( false );
209     }
210 
211     public static DBconnect getInstance(boolean isDynamic)
212   throws Exception
213     {
214   if ( isDynamic )
215     return openDynamicConnect();
216   else  
217        return openConnect();
218     }
219 
220     public static void close(DBconnect db_)
221     {
222   if ( db_.isDynamicConnect )
223   {
224             try {
225     db_.conn.close();
226     db_.conn = null;
227       }catch(Exception e){}
228   }
229     }
230 
231 
232 
233 /**
234   @deprecated
235 */
236     public static DBconnect get()
237   throws Exception
238     {
239   return getInstance( false );
240     }
241 
242 }